\yii\web\Application::EVENT_AFTER_REQUEST
\yii\web\Application::EVENT_BEFORE_REQUEST
还有哪些自带的事件,在哪里查找它们?
我现在需要对response数据进行修改,我自定义了一个行为,然后重载了events方法。
在这个方法返回一个数组,key为事件,value为handler
我现在需要对返回的数据进行修改,要用到哪个事件?
class MyBehavior extends Behavior
{
public function events()
{
return [
**KEY** => 'abc'
];
}
public static function abc($event)
{
$response = Yii::$app->getResponse();
print_r($response);
exit;
}
}
针对 我现在需要对response数据进行修改
, 可以这么做
return [
// ...
'components' => [
'response' => [
'class' => 'yii\web\Response',
'on beforeSend' => function ($event) {
$response = $event->sender;
if ($response->data !== null) {
$response->data = [
'success' => $response->isSuccessful,
'data' => $response->data,
];
$response->statusCode = 200;
}
},
],
],
];
上面的代码写到配置文件里, 是全局性的。
可以在上面绑定 event handle, 然后在别处写具体的处理逻辑, 参考Configuration Format:
[
'class' => 'app\components\SearchEngine',
'apiKey' => 'xxxxxxxx',
'on search' => function ($event) {
Yii::info("Keyword searched: " . $event->keyword);
},
'as indexer' => [
'class' => 'app\components\IndexerBehavior',
// ... property init values ...
],
]
事件都是在其所属类里定义的, 不想看文档, 就自己去代码找吧
如果编辑工具用的是 vim, 可以增加这个插件, 以方便查找文档
另外的类似的问题