【问题标题】:Yii2: how to use component in ActiveController default actionYii2:如何在 ActiveController 默认操作中使用组件
【发布时间】:2016-03-30 12:34:47
【问题描述】:

正如文档所说: [[yii\rest\IndexAction|index]]: list resources page by page

响应有视图:

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.

  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);
    }

响应内容正确但有视图:

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

我想使用来自组件的响应并逐页列出资源,因为它执行默认操作。应该怎么做才合适?

【问题讨论】:

    标签: php rest controller yii2


    【解决方案1】:

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

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

    您的操作应返回一个实现 DataProviderInterfacedata provider 对象,该对象可以是以下任何一种:

    所以这完全取决于 $tweetLastFinder-&gt;findLastTweets() 返回的对象类型。如果findLastTweets 方法返回一个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 实例中。如果它是一个更复杂的对象,那么您需要构建一个自定义数据提供程序,您可以在其中包装它。在相关的docs 中查看如何做到这一点。

    【讨论】:

    • 嗯.. 我不明白,为什么在默认操作中能够使用简单的 url?你回答中的动作是定制的吗?你的意思是我需要取消设置index
    • no not url 只是直接调用类而不在文件顶部声明其命名空间use \yii\data\ActiveDataProvider。我会进行更新以更好地解释。
    • 已更新。请注意,$count 在这里没有意义,因为序列化程序会从那里开始过滤和限制数据。您只需在请求中设置&amp;per-page=xx
    • 非常感谢。但是在所有情况下HTTP/1.1 500 Internal Server Error,我做错了什么?
    • 500 是一般的。它甚至可以是缺少; 的任何东西。当在 php.ini 中禁用 display_errors 时,服务器通常会显示而不是真正的错误消息(请参阅this)。真正的错误信息也可以在您的服务器的日志文件中找到。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多