Yii::$app->response->on(yii\web\Response::EVENT_BEFORE_SEND, function($event){ if(is_string($event->sender->data)){ $event->sender->data = '以下是HTML内容:' . $event->sender->data; } });
2种方法
通过Response的源代码可以得知EVENT_BEFORE_SEND这个常量的值是beforeSend
,或者你echo Yii::$app->response::EVENT_BEFORE_SEND
也是能看到这个值
return [ Application的配置 'components' => [ 其它组件的配置 'response' => [ //重点在这里,先写on加空格,然后就写事件的标记值 'on beforeSend' => function($event){ if(is_string($event->sender->data)){ $event->sender->data = '以下是HTML内容:' . $event->sender->data; } } ], ], ];