score:3

Accepted answer

Just add the container as an argument into your service definition.

services:
    kernel.listener.acme_listener:
        class: Acme\AcmeBundle\EventListener\AcmeListener
        arguments:
            - @service_container
        tags:
            - { name: kernel.event_listener, event: kernel.controller, method: onKernelController }

Then add a constructor to your AcmeListener class so you can access it.

use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;

class AcmeListener
{
    /**
     * @var Container
     */
    private $container;

    /**
     * Constructor
     *
     * @param Container $container
     */
    public function __construct(Container $container)
    {
            $this->container = $container;
    }

    public function onKernelController(FilterControllerEvent $event)
    {
            $this->container->...
    }
}

Related Query

More Query from same tag