我想将所有HTTP请求记录到站点,框架Yii。
我可以使用框架的标准功能吗?
答案 0 :(得分:2)
我们是使用onBeginRequest
或onEndRequest
完成的。在此处查看更多信息:How to use events in Yii
实质上,您将全局请求事件与您自己的类挂钩,然后执行您想要执行的操作。
答案 1 :(得分:1)
在Yii2中,事件被称为beforeRequest
和afterRequest
(http://www.yiiframework.com/doc-2.0/guide-structure-applications.html)
例如:
Yii::$app->on('afterRequest', function($event) {
//do something...
//response object is in $event->sender->response
//response data is in $event->sender->response->data
//request object is in $event->sender->request
});
但这是在响应数据准备好进入响应内容之前(根据响应格式,例如。Response::FORMAT_JSON
)所以如果你想要真正的最终响应体,你需要这样做(http://www.yiiframework.com/doc-2.0/guide-runtime-responses.html#sending-response):
Yii::$app->response->on(\yii\web\Response::EVENT_AFTER_SEND, function($event) { //alternatively use EVENT_AFTER_PREPARE
//now you can access $event->sender->content and eg. $event->sender->getHeaders()
//Also, you have access to the request through Yii::$app->request
});