Yii2 - API 休息 - ActiveDataProvider

Yii2 - API Rest - ActiveDataProvider

我正在使用 Yii2 基本模板构建 REST API。我收到一个错误:

exception 'yii\base\InvalidArgumentException' with message 'Response content must be a string or an object implementing __toString().' in /Users/aurasix/ASX-Startups/ASX-CMS/asx-api-yii/vendor/yiisoft/yii2/web/Response.php:1062

Copy Stacktrace Search Whosebug Search Google Exception
Invalid Argument – yii\base\InvalidArgumentException
Response content must be a string or an object implementing __toString().

我正在遵循 yii2 网站上的指南:https://www.yiiframework.com/doc/guide/2.0/en/rest-resources

尝试使用集合,以便将来使用分页和排序,我是否遗漏了什么?

我知道如果我使用 ActiveController 可能会更容易,但我想了解完整的过程,这就是我使用 Controller 的原因。另外我想要完全控制,我认为 ActiveController 将通过定义模型来发布所有方法,对吗?

我的控制器不是从 ActiveController 而是从 Controller 扩展的

namespace app\modules\v1\controllers;

use yii\web\Controller;
use app\modules\v1\models\Blog;
use yii\data\ActiveDataProvider;

class BlogController extends Controller {

    public $serializer = [
        'class' => 'yii\rest\Serializer',
        'collectionEnvelope' => 'items',
    ];

    public function actionIndex() {
        return new ActiveDataProvider([
            'query' => Blog::find()
        ]);
    }

}

我的模特:

namespace app\modules\v1\models;

use yii\db\ActiveRecord;
use yii\web\Linkable;

class Blog extends ActiveRecord implements Linkable {

    public static function tableName() {
        return 'blog_post';
    }

    public function fields() {
        return [
            'id',
            'slug',
            'title',
            'full_content'
        ];
    }

    public function extraFields() {
        return [
            'publish_date',
            'short_content'
        ];
    }

    public function getLinks() {
        return [

        ];
    }

}

在config.php

'response' => [
            'formatters' => [
                \yii\web\Response::FORMAT_JSON => [
                    'class' => 'yii\web\JsonResponseFormatter',
                    'prettyPrint' => YII_DEBUG, // use "pretty" output in debug mode
                    'encodeOptions' => JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE,
                ],
            ],
        ],
'urlManager' => [
            'enablePrettyUrl' => false,
            'showScriptName' => false,
            'rules' => [
                [
                    'class' => 'yii\rest\UrlRule',
                    'controller' => 'v1/blog',
                    'pluralize'=>false
                ]
            ],
        ]

您可能应该使用 yii\rest\Controller 作为基础控制器 class。它不会像 yii\rest\ActiveController 那样为您提供所有的魔法,但它包含一些基本的请求过滤和响应格式化功能。

yii\web\Controller不包含$serializer属性,它不会序列化你的动作响应,所以你不能在动作方法中returnActiveDataProvider。 您应该查看 yii\rest\Controller source code - 它使用 afterAction() 从操作中序列化 ActiveDataProvider returned。没有它,您无法在操作方法中通过 $serializer 属性 或 return ActiveDataProvider 配置序列化程序。

Yii REST 服务主要为您提供 2 种类型的控制器,它们是

  • yii\rest\ActiveController
  • yii\rest\Controller

您需要从 yii\rest\Controller 而不是 yii\web\Controller 扩展您的控制器,因为 yii\web\Controller that you are trying to specify but for yii\rest\Controller [=18] 没有名称为 $serializer 的 属性 =]