gpt4 book ai didi

php - Yii2模块自定义响应类

转载 作者:可可西里 更新时间:2023-11-01 13:23:57 24 4
gpt4 key购买 nike

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

在 Controller 操作中,我返回了一组结果,但未使用自定义响应类。

相反,使用的类是默认的 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',
],
],
],

在 Controller 中我编辑了行为方法:

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"
}
]}

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

我需要做什么才能在操作中返回数组并使用自定义响应组件?

提前致谢

最佳答案

yii\base\Module 没有响应组件,所以你的配置将不起作用。与其将 response 组件添加到您的模块中,不如在 MyMod::init() 函数中更改 Yii::$app->response

如果你想用你自己的组件完全替换 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
];
});
}

关于php - Yii2模块自定义响应类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38793933/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com