【问题标题】:Yii2 - API Rest - ActiveDataProviderYii2 - API 休息 - ActiveDataProvider
【发布时间】:2018-04-13 21:47:50
【问题描述】:

我正在使用 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 Stackoverflow 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
                ]
            ],
        ]

【问题讨论】:

    标签: php yii2 yii2-api


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      Yii REST Services 主要为您提供 2 种控制器,分别是

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

      您需要从yii\rest\Controller 而不是yii\web\Controller 扩展您的控制器,因为您尝试指定的yii\web\Controller 没有名称为$serializer 的属性,但yii\rest\Controller 没有属性

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多