title: Access to database in a listener in Symfony 2
body: <p>We need to access to database info in a listener.
  We configure the listener in a service.yml
  The listener is like:</p>
  
  <pre><code>namespace company\MyBundle\Listener;
  
  use Symfony\Component\HttpKernel\Event\GetResponseEvent;
  use Symfony\Component\HttpKernel\HttpKernelInterface;
  use Symfony\Component\DependencyInjection\ContainerInterface;
  use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  use Symfony\Component\HttpFoundation\RedirectResponse;
  use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  
  class RequestListener
  {
      protected $container;
  
  public function __construct(ContainerInterface $container)
  {
      $this-&gt;container = $container;
  }
  
  public function onKernelRequest(GetResponseEvent $event)
  {
  ...
  </code></pre>
  
  <p>How can we access to doctrine at onKernelRequest function?</p>
  
  <p>I tried to extends from controller and do:</p>
  
  <pre><code>        $em = $this-&gt;getDoctrine()-&gt;getEntityManager(); 
  </code></pre>
  
  <p>and it works but I think this is a bad practice.</p>