jbsound
jbsound

Reputation: 787

Symfony2: Pass _route To Event Listener Service

I need to pass the '_route' from the container into an event listener, along with a route attribute. In other words, I need the listener service to become container aware without going through the overhead of passing the whole service_container into the event listener class.

I've seen code examples similar to this:

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

In a controller I would use something like the code below to grab the "_route":

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

However, in the event listener, I don't have that available.

How can I accomplish the above?

Thanks,

JB

Upvotes: 0

Views: 366

Answers (1)

Lusitanian
Lusitanian

Reputation: 11132

Set scope to request on the service xml tag. That will automatically inject the Request object, then set @request as an argument. In yaml, set scope: request and arguments: [@request].

Upvotes: 1

Related Questions