Php Yii2:如何在ActiveController默认操作中使用组件

Php Yii2:如何在ActiveController默认操作中使用组件,php,rest,controller,yii2,Php,Rest,Controller,Yii2,正如文件所说: [[yii\rest\IndexAction | index]]:逐页列出资源 回应如下: curl -i -H "Accept:application/json" "http://192.168.100.5/index.php/tweets" HTTP/1.1 200 OK Date: Wed, 30 Mar 2016 12:10:07 GMT Server: Apache/2.4.7 (Ubuntu) X-Powered-By: PHP/5.5.9-1ubuntu4.14 X

正如文件所说:
[[yii\rest\IndexAction | index]]:逐页列出资源

回应如下:

curl -i -H "Accept:application/json" "http://192.168.100.5/index.php/tweets"
HTTP/1.1 200 OK
Date: Wed, 30 Mar 2016 12:10:07 GMT
Server: Apache/2.4.7 (Ubuntu)
X-Powered-By: PHP/5.5.9-1ubuntu4.14
X-Pagination-Total-Count: 450
X-Pagination-Page-Count: 23
X-Pagination-Current-Page: 1
X-Pagination-Per-Page: 20
Link: <http://192.168.100.5/tweets?page=1>; rel=self, <http://192.168.100.5/tweets?page=2>; rel=next, <http://192.168.100.5/tweets?page=23>; rel=last
Content-Length: 4305
Content-Type: application/json; charset=UTF-8

[{"id":71,"text":"Juíza do RS Graziela Bünd.......
curl -i -H "Accept:application/json" "http://192.168.100.5/index.php/tweets"
HTTP/1.1 200 OK
Date: Wed, 30 Mar 2016 12:15:36 GMT
Server: Apache/2.4.7 (Ubuntu)
X-Powered-By: PHP/5.5.9-1ubuntu4.14
Content-Length: 2282
Content-Type: application/json; charset=UTF-8

[{"id":605,"text":"Popular Mus......
响应具有正确的内容,但具有视图:

curl -i -H "Accept:application/json" "http://192.168.100.5/index.php/tweets"
HTTP/1.1 200 OK
Date: Wed, 30 Mar 2016 12:10:07 GMT
Server: Apache/2.4.7 (Ubuntu)
X-Powered-By: PHP/5.5.9-1ubuntu4.14
X-Pagination-Total-Count: 450
X-Pagination-Page-Count: 23
X-Pagination-Current-Page: 1
X-Pagination-Per-Page: 20
Link: <http://192.168.100.5/tweets?page=1>; rel=self, <http://192.168.100.5/tweets?page=2>; rel=next, <http://192.168.100.5/tweets?page=23>; rel=last
Content-Length: 4305
Content-Type: application/json; charset=UTF-8

[{"id":71,"text":"Juíza do RS Graziela Bünd.......
curl -i -H "Accept:application/json" "http://192.168.100.5/index.php/tweets"
HTTP/1.1 200 OK
Date: Wed, 30 Mar 2016 12:15:36 GMT
Server: Apache/2.4.7 (Ubuntu)
X-Powered-By: PHP/5.5.9-1ubuntu4.14
Content-Length: 2282
Content-Type: application/json; charset=UTF-8

[{"id":605,"text":"Popular Mus......
在这种情况下,我不能使用
$serializer
、show
\u meta


我希望使用来自组件的响应,并按照默认操作逐页列出资源。如何正确地完成它?

要充分利用内置并显示
\u meta
或使您的URL看起来像:

/tweets?page=5&per-page=12&sort=name
您的操作应返回一个实现的对象,该对象可以是以下任一对象:

  • 一个
  • 一个
  • 或者
因此,这完全取决于
$tweetLastFinder->findLastTweets()
返回的对象类型。如果
findlastweets
方法返回一个
ActiveQuery
对象,如:

public function findLastTweets($count)
{
    ...
    return $Tweets::find();
}
然后将其放入一个
ActiveDataProvider
实例:

use yii\data\ActiveDataProvider;

public function actionIndex($count = 10)
{
    /** @var TweetLastfinder $tweetLastFinder */
    $tweetLastFinder = Yii::$app->get('tweetlastfinder');

    $tweets = $tweetLastFinder->findLastTweets();

    return new ActiveDataProvider([
        'query' => $tweets,
    ]);
}

如果它返回一个数据数组或可以转换为数组的东西,那么只需将其放入
ArrayDataProvider
实例中即可。如果它是一个更复杂的对象,那么您需要构建一个自定义数据提供程序,在其中可以封装它。请参见相关章节中的操作方法。

Hmm。。我不明白,为什么在默认操作中可以使用简单的url?你的答案中的行动是定制的吗?您的意思是我需要取消设置
索引
?不需要url,只需直接调用类,而不在文件顶部声明其名称空间
use\yii\data\ActiveDataProvider
。我会做更新来更好地解释。更新。请注意,
$count
在这里没有意义,因为序列化程序将从这里开始过滤和限制数据。您只需在请求中设置
&每页=xx
。非常感谢。但是在所有情况下
HTTP/1.1500内部服务器错误
,我做错了什么?500是通用的。它可以是任何东西,甚至是缺少的
。当php.ini中禁用display_errors时,通常由服务器显示而不是真实的错误消息(请参阅)。真正的错误消息也可以在服务器的日志文件中找到。