在我的Symfony2.2应用程序中,我使用一个onKernelTerminate事件监听器,以便在响应已呈现后进行一段“繁重”的处理,从而使用户获得更快的响应时间。在我的控制器中,我设置了请求上的属性,以便在事件监听器运行时,可以使用该属性来决定是否存在某些内容并对其进行处理。这在我的开发环境和未启用AppCache的测试环境中完美地工作。但是,一旦我启用了AppCache(在我的app.php中)-我想这样做是因为我将其用于所有的HTTP缓存,事件监听器就会停止工作,因为似乎通过onKernelTerminate事件访问的请求具有一个空的属性参数包。在控制器中设置的属性在PostResponseEvent中的请求中不可用。为什么AppCache会产生这种影响?是否有其他方法可以在控制器和事件监听器之间传递数据,并且可以与AppCache一起使用?以下是一小段代码,供您参考:
#in my controller
$request->attributes->set('listener-to-process', 'test');
#in my EventListener:
public function onKernelTerminate(PostResponseEvent $event) {
$request = $event->getRequest();
//use this request attribute to decide what to process
if (!$request->attributes->has('listener-to-process')) {
return;
};
$type = $request->attributes->get('listener-to-process');
$status = $this->api->persistTag(type);
return;
}