title: getUser Entity in EventSubscriber
body: <p>I began to build my first web application with symfony. In this app I got user management with authentification and multilanguage. In the user database tabel is a lang column for each user. I got the changing of the default language for the app by changing it through a _GET parameter and also by the login by the database value running.
  Now I want to change the value in the database automatically by switching the language through the URL _GET parameter in the EventSubscriber.
  Unfortunately I got no idea how to get the user entity in my 'onKernelRequest' function to save the selecte language in the database.
  All the logic of changing the language is done in the following code:</p>
  
  <pre><code>&lt;?php
  namespace App\EventSubscriber;
  
  use Symfony\Component\HttpFoundation\Session\SessionInterface;
  use Symfony\Component\HttpKernel\Event\GetResponseEvent;
  use Symfony\Component\HttpKernel\KernelEvents;
  use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  use Symfony\Component\Security\Http\SecurityEvents;
  use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
  
  class LocaleSubscriber implements EventSubscriberInterface
  {
      private $defaultLocale;
      private $allowedLangs;
      private $session;
  
      public function __construct(SessionInterface $session, $defaultLocale = 'de')
      {
          $this-&gt;defaultLocale = $defaultLocale;
          $this-&gt;allowedLangs = ['de', 'en'];
          $this-&gt;session = $session;
      }
  
      public function onInteractiveLogin(InteractiveLoginEvent $event)
      {
          $user = $event-&gt;getAuthenticationToken()-&gt;getUser();
  
          if (null !== $user-&gt;getLocale()) {
              $this-&gt;session-&gt;set('_locale', $user-&gt;getLocale());
          }
      }
  
      public function onKernelRequest(GetResponseEvent $event)
      {
          $request = $event-&gt;getRequest();
          if (!$request-&gt;hasPreviousSession()) {
              return;
          }
  
          // try to see if the locale has been set as a _locale routing parameter
          if ($locale = $request-&gt;attributes-&gt;get('_locale')) {
              $request-&gt;getSession()-&gt;set('_locale', $locale);
          } elseif(array_key_exists('_locale', $_GET) &amp;&amp; array_search($_GET['_locale'], $this-&gt;allowedLangs) !== FALSE) {
              $request-&gt;setLocale($_GET['_locale']);
              $request-&gt;getSession()-&gt;set('_locale', $_GET['_locale']);
          } else {
              // if no explicit locale has been set on this request, use one from the session
              $request-&gt;setLocale($request-&gt;getSession()-&gt;get('_locale', $this-&gt;defaultLocale));
          }
      }
  
      public static function getSubscribedEvents()
      {
          return array(
              // must be registered before (i.e. with a higher priority than) the default Locale listener
              SecurityEvents::INTERACTIVE_LOGIN =&gt; array(array('onInteractiveLogin', 15)),
              KernelEvents::REQUEST =&gt; array(array('onKernelRequest', 20)),
          );
      }
  }
  </code></pre>
  
  <p>Thanks in advance
  Peter</p>