0

我需要将容器中的“_route”以及路由属性传递给事件侦听器。换句话说,我需要侦听器服务能够感知容器,而无需通过将整个 service_container 传递到事件侦听器类的开销。

我见过类似这样的代码示例:

services: 
    root.path.locator: 
        class: Acme\Bundle\HelloBundle\Util\RootLocator 
        arguments: ['%kernel.root_dir%'] 

在控制器中,我会使用类似下面的代码来获取“_route”:

$request = $this->container->get('request');
$routeName = $request->get('_route');

但是,在事件侦听器中,我没有可用的。

我怎样才能完成上述工作?

谢谢,

JB

4

1 回答 1

1

在服务 xml 标记上设置scope为。request这将自动注入 Request 对象,然后将 @request 设置为参数。在 yaml 中,设置scope: requestarguments: [@request].

于 2012-07-12T00:03:11.833 回答