Yii2:Rest API 返回 xml 而不是 json

问题描述

我刚刚使用 Yii2 构建了一个应用程序作为 Flutter 应用程序的后端

so .. 我创建了一个 modules/api 文件夹,并在其中创建了控制器,就像这样

<?PHP

 namespace app\modules\api\controllers;

 use yii\web\Controller;
 use yii\rest\ActiveController;


 class AdController extends ActiveController
 {
   public  $modelClass = 'app\models\Ad';
 }

它工作正常,但它返回 XML

我在 web.PHP 中尝试过

'components' => [
    'response' => [
        'format' => \yii\web\Response::FORMAT_JSON,],

'request' => [
        'parsers' => [
            'application/json' => 'yii\web\JsonParser',]
    ],

但它仍然返回 XML

更新

当我使用

        'urlManager' => [ 
        ....
        'enableStrictParsing' => true,...
         ]

它给我 未找到 (#404)

解决方法

首先创建一个基本控制器

并使用此配置覆盖行为方法

launchd       1             root    8u  IPv6 0xbe03e516a8f8ef2f      0t0  TCP *:22 (LISTEN) launchd       1             root    9u  IPv4 0xbe03e516a8f9797f      0t0  TCP *:22 (LISTEN) launchd       1             root   11u  IPv6 0xbe03e516a8f8ef2f      0t0  TCP *:22 (LISTEN) launchd       1             root   12u  IPv4 0xbe03e516a8f9797f      0t0  TCP *:22 (LISTEN) launchd       1             root   24u  IPv6 0xbe03e516bbf55a6f      0t0  TCP localhost:8021 (LISTEN) launchd       1             root   26u  IPv4 0xbe03e516d869f97f      0t0  TCP localhost:8021 (LISTEN) launchd       1             root   38u  IPv6 0xbe03e516bbf55a6f      0t0  TCP localhost:8021 (LISTEN) launchd       1             root   39u  IPv4 0xbe03e516d869f97f      0t0  TCP localhost:8021 (LISTEN) UserEvent   118             root   83u  IPv6 0xbe03e516a8f8d6af      0t0  TCP [fe80:4::aede:48ff:fe00:1122]:49157 (LISTEN) UserEvent   118             root   84u  IPv6 0xbe03e516a8f9018f      0t0  TCP [fe80:4::aede:48ff:fe00:1122]:49158 (LISTEN) UserEvent   118             root   85u  IPv6 0xbe03e516a8f907af      0t0  TCP [fe80:4::aede:48ff:fe00:1122]:49159 (LISTEN) UserEvent   118             root   86u  IPv6 0xbe03e516a8f8ca6f      0t0  TCP [fe80:4::aede:48ff:fe00:1122]:49160 (LISTEN) UserEvent   118             root   88u  IPv6 0xbe03e516a8f8c44f      0t0  TCP [fe80:4::aede:48ff:fe00:1122]:49161 (LISTEN) mysqld      182           _mysql   29u  IPv6 0xbe03e516a8f8dccf      0t0  TCP *:33060 (LISTEN) mysqld      182           _mysql   31u  IPv6 0xbe03e516a8f8d08f      0t0  TCP *:3306 (LISTEN) mDNSRespo   272   _mdnsresponder   33u  IPv4 0xbe03e516d869d1ff      0t0  TCP *:53 (LISTEN) mDNSRespo   272   _mdnsresponder   65u  IPv6 0xbe03e516d9bd56af      0t0  TCP *:53 (LISTEN) ir_agent    338             root   10u  IPv4 0xbe03e516a8f965bf      0t0  TCP localhost:49155 (LISTEN) Spotify     795 nimish.prabhakar   59u  IPv4 0xbe03e516a968df9f      0t0  TCP *:59124 (LISTEN) Spotify     795 nimish.prabhakar  125u  IPv4 0xbe03e516beeb135f      0t0  TCP *:57621 (LISTEN) mongod      993 nimish.prabhakar   10u  IPv4 0xbe03e516bb03dd3f      0t0  TCP localhost:27017 (LISTEN) PanGPS     1000             root    7u  IPv4 0xbe03e516bb03c97f      0t0  TCP localhost:4767 (LISTEN) postgres   1148 nimish.prabhakar    7u  IPv6 0xbe03e516bbf566af      0t0  TCP localhost:5432 (LISTEN) postgres   1148 nimish.prabhakar    8u  IPv4 0xbe03e516bb03e71f      0t0  TCP localhost:5432 (LISTEN) Adobe\x20  1217 nimish.prabhakar    9u  IPv4 0xbe03e516a8f9971f      0t0  TCP localhost:15292 (LISTEN) node       1258 nimish.prabhakar   26u  IPv4 0xbe03e516bb03a1ff      0t0  TCP localhost:49222 (LISTEN) node       1258 nimish.prabhakar   28u  IPv4 0xbe03e516bc0205bf      0t0  TCP localhost:49223 (LISTEN) node       1258 nimish.prabhakar   29u  IPv4 0xbe03e516bc020f9f      0t0  TCP localhost:45623 (LISTEN) node       1258 nimish.prabhakar   30u  IPv4 0xbe03e516bb03abdf      0t0  TCP localhost:52739 (LISTEN) com.docke  1350 nimish.prabhakar   11u  IPv4 0xbe03e516bc02371f      0t0  TCP localhost:49238 (LISTEN) dcagentse 46158             root    9u  IPv4 0xbe03e516d1d545bf      0t0  TCP localhost:4990 (LISTEN)

然后在你所有的项目控制器中扩展它

contentNegotiator 键负责响应格式

,

您可以尝试在控制器操作中放置:

\Yii::$app->response->format = \yii\web\Response::FORMAT_JSON

例如:

public function actionAd()
{
    \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;

    // return response
}

但是,您应该为 serialize

添加类
 class AdController extends ActiveController
 {
     public $modelClass = 'app\models\Ad';
     public $serializer = [
         'class' => 'yii\rest\Serializer','collectionEnvelope' => 'items',];
 }