Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2: How to get a config parameter within a listener?

I have a listener service. I want to read some config parameters inside it.

How can I access the service container within the listener class?

like image 229
Amit Avatar asked May 03 '12 05:05

Amit


1 Answers

You can pass container parameters into your service by using the %your_param_name% notation:

services:
    kernel.listener.locale_listener:
        class: My\BundleName\Listener\LocaleListener
        tags:
            - { name: kernel.event_listener, event: kernel.request, method: onKernelRequest }
        arguments: [ @router, @service_container, %your_param_name% ]

which will present itself as (in this example) the 3rd argument in your service's constructor method:

public function __construct($router, $container, $paramValue)
{
    // ...
}
like image 131
richsage Avatar answered Sep 21 '22 15:09

richsage