Symfony2:为什么请求传递给 Symfony2 中受 AppCache 影响的 Kernel.Terminate EventListener [英] Symfony2: Why is the Request passed to a Kernel.Terminate EventListener affected by AppCache in Symfony2

查看:20
本文介绍了Symfony2:为什么请求传递给 Symfony2 中受 AppCache 影响的 Kernel.Terminate EventListener的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 Symfony2.2 应用程序中,我使用了一个 onKernelTerminate EventListener,这样我就可以在我的响应呈现后进行一些繁重"的处理,以便用户获得更快的响应时间.

In my Symfony2.2 application I am using an onKernelTerminate EventListener so that I can do a piece of "heavy" processing after my response has been rendered so the user receives a faster response time.

在我的控制器中,我为请求设置了一个属性,这样当事件侦听器运行时,该属性可用于决定是否有某些内容以及要处理的内容.

In my controller I set an attribute on the request so that when the event listener runs, that attribute can be used to decide if there is something and what to process.

这在我的开发环境和未启用 AppCache 的测试环境中完美运行.但是一旦我启用了 AppCache(在我的 app.php 中)——我想这样做是因为我将它用于我的所有 HTTP 缓存——事件侦听器停止工作,因为我通过 onKernelTerminate 中的事件访问的请求似乎有一个空属性参数包.

This works perfectly on my dev environment and on a test environment where AppCache is not enabled. But as soon as I enable AppCache (in my app.php) - which I want to do because I use it for all my HTTP caching - the event listener stops working because it seems the Request that I access via the Event in onKernelTerminate has an empty attributes parameter bag.

我在控制器中设置的属性在 PostResponseEvent 中的请求中不可用.

The attribute I set in the controller isn't available from the request in the PostResponseEvent.

AppCache 为什么会有这个效果?是否有另一种方法可以在我的控制器和可与 AppCache 一起使用的事件侦听器之间传递数据?

Why does AppCache have this effect? And is there an alternative way to pass data between my controller and an Event Listener which would work with AppCache?

这是一段代码,您可以看到:

Here's a snippet of code so you can see:

#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;

}

推荐答案

我找到了答案 - 我的困惑源于不知道如何在单个请求中在服务之间传递信息.AppCache 有点乱(虽然我仍然不知道为什么它会重置对请求的更改).

I found the answer - and my confusion came from not knowing how to pass information between services in a single request. AppCache was a bit of a red-herring (though I still don't know why it resets changes to the request).

我认为唯一的方法是设置请求的属性,以便侦听服务可以从请求中提取.事实上,服务在请求中是持久的——所以服务本身可以跟踪它在 onKernelTerminate 事件之后需要做什么.

I thought the only way was to set properties of the request so that the listening service could pull from the request. In fact services are persistent within the request - so the service itself can keep track of what it needs to do after the onKernelTerminate event.

我创建了服务的私有属性(例如 $itemsToPersist),并在此过程中将项目添加到该数组中.

I created a private property of the service (eg $itemsToPersist) and during the course of the process add items to that array.

然后在监听器的 onKernelTerminate 方法中:

Then in the listener onKernelTerminate method:

$service = $this->container->get('your_service');
$service->persistItems();

只需调用可以循环遍历 itemsToPersist 数组的方法并执行它需要执行的任何操作.

just call the method which can loop through the itemsToPersist array and do whatever it needs to do.

无需在请求中传递内容!

No need to pass stuff in the request!

这篇关于Symfony2:为什么请求传递给 Symfony2 中受 AppCache 影响的 Kernel.Terminate EventListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆