Yii2:如何在ActiveController中使用组件默认动作 [英] Yii2: how to use component in ActiveController default action

查看:440
本文介绍了Yii2:如何在ActiveController中使用组件默认动作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如文档所示:
[[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.......

我有一个组件,其中一个返回 - 一些数组(从两个表选择)如果我自定义indexAction。

I have component which one return - some array (selection from two tables). If i customize indexAction.

  public function actions()
    {
        $actions = parent::actions();
        unset($actions['update']);
        unset($actions['delete']);
        unset($actions['view']);
       unset($actions['index']);

        return $actions;
    }

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

        return $tweetLastFinder->findLastTweets($count);
    }

回应内容正确,但有查看:

Response have correct content but has view:

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 ,显示 _meta

In this case i cant use $serializer, show _meta etc

我想要使用组件和列表资源的响应,因为它是默认动作,应该怎么做?

I want to use response from component and list resources page by page as it do default action. How it should be done properly?

推荐答案

要充分利用内置的 yii \rest\Serializer 并显示 _meta 或让您的网址看起来像:

To get full use of the built in yii\rest\Serializer and show _meta or have your urls to look like:

/tweets?page=5&per-page=12&sort=name

您的操作应返回数据提供者对象,用于实现 DataProviderInterface ,可以是以下任何一种:

your action should return a data provider object that implements the DataProviderInterface which can be any of those:

所以这一切都取决于什么样的对象 $ tweetLastFinder-> findLastTweets()正在返回。如果 findLastTweets 方法返回 ActiveQuery 对象,如:

So it all depends on what kind of object $tweetLastFinder->findLastTweets() is returning. If the findLastTweets method is returning an ActiveQuery object like:

public function findLastTweets($count)
{
    ...
    return $Tweets::find();
}

然后将它放入 ActiveDataProvider instance:

Then just put it into an ActiveDataProvider instance:

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 实例。如果它是一个更复杂的对象,那么您需要构建一个自定义数据提供程序,您可以在其中包装它。请参阅相关 docs

If it returns an array of data or something you can convert to an array then just put it into an ArrayDataProvider instance. If it is a more complex object then you'll need to build a custom Data Provider inside which you can wrap it. See how to do that in related docs.

这篇关于Yii2:如何在ActiveController中使用组件默认动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆