title: Symfony2 get userID on kernel.controller event listener
body: <p>Can someone help me to get userID in event listener from event.controller?</p>
  
  <pre><code>#   EventListener
      kernel.listener.corporation.manage:
          class:  Site\CorporationBundle\Event\SiteCorporationManageListener
          arguments: ["@doctrine.orm.entity_manager", "@user.own.item", "@security.context"]
          tags:
              - { name: kernel.event_listener, event: kernel.controller, method: onKernelRequest }
  </code></pre>
  
  <p>And listener class</p>
  
  <pre><code>use Doctrine\ORM\EntityManager;
  use Site\MainBundle\Service\UserOwnItem;
  use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
  use Symfony\Component\HttpKernel\HttpKernelInterface;
  use Symfony\Component\Security\Core\SecurityContext;
  
  class SiteCorporationManageListener
  {
      private $oEntityManager = null;
      private $oUserOwnItem = null;
      private $oSecurityContext = null;
  
      public function __construct(EntityManager $oEntityManager, UserOwnItem $oUserOwnItem, SecurityContext $oSecurityContext)
      {
          $this-&gt;oEntityManager = $oEntityManager;
          $this-&gt;oUserOwnItem = $oUserOwnItem;
          $this-&gt;oSecurityContext = $oSecurityContext;
      }
  
      public function onKernelRequest(FilterControllerEvent $event)
      {
          if (HttpKernelInterface::MASTER_REQUEST === $event-&gt;getRequestType()) {
              $route = $event-&gt;getRequest()-&gt;get('_route');
              $corporationID = $event-&gt;getRequest()-&gt;get('corporationID', null);
              $userID = $this-&gt;oSecurityContext-&gt;getToken()-&gt;getUser()-&gt;getID();
              //$userID = 3;
              //var_dump($userID);
  
              if (strstr($route, 'corporation')) {
                  if (!strstr($route, 'corporation_index')) {
                      $bUserOwn = $this-&gt;oUserOwnItem-&gt;setUserID($userID)-&gt;setItemType('corporation')-&gt;setItemID($corporationID)-&gt;userOwn();
                      //var_dump($bUserOwn);
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>I'll clean it later, i try different ways to do it, but even through container and security_context i cannot get userID. It brokes after getToken() method =.</p>
  
  <p>At this example $userID is null... Even after getToken()->getUser() give me null... </p>
  
  <pre><code>FatalErrorException: Error: Call to a member function getUser() on a non-object in /home/dev/public_html/git.eve-ceo/src/Site/CorporationBundle/Event/SiteCorporationManageListener.php line 32
  </code></pre>
  
  <p>Help please.</p>