Symfony2 制作容器感知事件分发器

Symfony2 Making a Container-Aware Event Dispatcher

我想在触发自定义事件时在我的 Symfony2 应用程序的内核上设置 HTTP 请求(类似于 Symfony\Component\HttpKernel\Event\GetResponseEvent 在内核上设置 HTTP 响应的方式)。

我关注了Event Listeners cookbook and I build a Request, but my custom event doesn't have access to the kernel to pass the Request to. Further reading suggests I need a container aware event dispatcher and there is a quick guide here

在设置下,指南显示 "Setup is straightforward by injecting a ContainerInterface into the ContainerAwareEventDispatcher"。

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher;

$container = new ContainerBuilder();
$dispatcher = new ContainerAwareEventDispatcher($container);

该代码在我的应用程序中的什么位置?

您搞混了,因为您正在尝试应用针对裸事件调度程序组件的指令。

只需在您的事件侦听器服务中注入容器:

my_listener:
    calls:
        - [setContainer, [@service_container]]