Yii2模块自定义响应类

喂养

我已经定义了一个自定义响应类,并试图在模块中使用它。

在控制器操作中,我返回结果数组,但未使用自定义响应类。

相反,使用的类是默认的yii \ web \ Response

我的实施

config / web.php中的模块配置:

'mymodule' => [
        'class' => 'app\modules\mymod\Mymod',
        'components' => [                
            'response' => [
                'class' => 'app\modules\mymod\components\apiResponse\ApiResponse',
                'format' => yii\web\Response::FORMAT_JSON,
                'charset' => 'UTF-8',
            ],
        ],
    ],

在控制器中,我编辑了behaviors方法:

public function behaviors() {
    $behaviors = parent::behaviors();        
    $behaviors['contentNegotiator'] = [
        'class' => 'yii\filters\ContentNegotiator',            
        'response' => $this->module->get('response'),                
        'formats' => [  //supported formats
            'application/json' => \yii\web\Response::FORMAT_JSON,
        ],
    ];
    return $behaviors;
}

在行动中,如果我这样做:

public function actionIndex() {

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

    $dataList = [
        ['id' => 1, 'name' => 'John', 'surname' => 'Davis'],
        ['id' => 2, 'name' => 'Marie', 'surname' => 'Baker'],
        ['id' => 3, 'name' => 'Albert', 'surname' => 'Bale'],
    ];
    return $dataList;
}

我得到此结果(如预期的那样yii \ web \ Response):

[
{
    "id": 1,
    "name": "John",
    "surname": "Davis"
},
{
    "id": 2,
    "name": "Marie",
    "surname": "Baker"
},
{
    "id": 3,
    "name": "Albert",
    "surname": "Bale"
}
]

但是,如果我将操作更改为此:

$dataList = [
    ['id' => 1, 'name' => 'John', 'surname' => 'Davis'],
    ['id' => 2, 'name' => 'Marie', 'surname' => 'Baker'],
    ['id' => 3, 'name' => 'Albert', 'surname' => 'Bale'],
];
//return $dataList;

$resp = $this->module->get('response'); //getting the response component from the module configuration
$resp->data = $dataList;

return $resp;

然后我得到了预期的结果,这是:

{
"status": {
    "response_code": 0,
    "response_message": "OK",
    "response_extra": null
},
"data": [
    {
        "id": 1,
        "name": "John",
        "surname": "Davis"
    },
    {
        "id": 2,
        "name": "Marie",
        "surname": "Baker"
    },
    {
        "id": 3,
        "name": "Albert",
        "surname": "Bale"
    }
]}

看来我定义的行为没有做任何事情。

我只需要在操作中返回数组并使用自定义响应组件,该怎么做?

提前致谢

Oakymax

yii\base\Module没有响应组件,因此您的配置将无法正常工作。您无需response在模块中添加组件,而是可以Yii::$app->responseMyMod::init()函数内部进行更改

如果要完全替换Yii::$app->response为自己的组件,请执行以下操作:

public function init()
{
    parent::init();

    \Yii::configure(\Yii::$app, [
        'components' => [
            'response' => [
                'class' => 'app\modules\mymod\components\apiResponse\ApiResponse',
                'format' => yii\web\Response::FORMAT_JSON,
                'charset' => 'UTF-8',
            ],
        ]
    ]);
}

但是我认为在模块中完全替换父应用程序的Response组件是一个坏主意。更好的方法是根据需要修改“响应”行为。例如,您可以使用EVENT_BEFORE_SEND和构建自己的数据结构作为响应:

public function init()
{
    parent::init();

    // you can use ContentNegotiator at the level of module
    // and remove this behavior declaration from controllers
    \Yii::configure($this, [
        'as contentNegotiator' => [
            'class' => 'yii\filters\ContentNegotiator',
            // if in a module, use the following IDs for user actions
            // 'only' => ['user/view', 'user/index']
            'formats' => [
                'application/json' => Response::FORMAT_JSON,
            ],
        ],
    ]);


    // you can daclare handler as function in you module and pass it as parameter here
    \Yii::$app->response->on(Response::EVENT_BEFORE_SEND, function ($event) {
        $response = $event->sender;
        // here you can get and modify everything in current response 
        // (data, headers, http status etc.)
        $response->data = [
            'status' => 'Okay',
            'data' => $response->data
        ];
    });
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Yii2 Rest Api自定义错误响应

来自分类Dev

Yii2 Rest Api自定义错误响应

来自分类Dev

yii2 checkboxList自定义类

来自分类Dev

yii2 checkboxList自定义类

来自分类Dev

Yii2。自定义类验证器

来自分类Dev

Yii2。应用自定义URL规则类

来自分类Dev

使用Yii2中的模块的自定义URL规则

来自分类Dev

如何在Yii2中使用自定义的CSS文件进行模块布局

来自分类Dev

Yii2:从视图中调用类User中的自定义方法

来自分类Dev

如何在Yii2表单字段中为标签添加自定义类?

来自分类Dev

在Yii2中调用自定义组件(找不到类)

来自分类Dev

如何在自定义 Yii2 UrlRule 中查看类是否是 ActiveController 的子类

来自分类Dev

自定义错误类作为节点模块发送自定义错误响应

来自分类Dev

Yii2 GridView自定义标题行

来自分类Dev

我如何自定义Yii2 gridview排序?

来自分类Dev

使用Ajax的YII2 gridview自定义搜索

来自分类Dev

在yii2中自定义网格视图

来自分类Dev

Yii2自定义http异常视图

来自分类Dev

如何自定义yii2主题

来自分类Dev

Yii2扩展/自定义GridView

来自分类Dev

Yii2中ListView的自定义排序器

来自分类Dev

Yii2中的自定义图标

来自分类Dev

自定义按钮“操作”列,yii2

来自分类Dev

Yii2自定义登录注销

来自分类Dev

Yii2联合查询的自定义分页

来自分类Dev

yii2自定义REST API问题

来自分类Dev

YII2:搜索模型中的自定义排序

来自分类Dev

使用自定义模板yii2的小巧机身

来自分类Dev

在yii2中自定义网格视图