src/EventSubscriber/SetUserLangSubscriber.php line 24

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  4. use Symfony\Component\Security\Core\Event\AuthenticationSuccessEvent;
  5. use Symfony\Component\HttpFoundation\RequestStack;
  6. class SetUserLangSubscriber implements EventSubscriberInterface
  7. {
  8.     private $requestStack;
  9.     public function __construct(RequestStack $requestStack)
  10.     {
  11.         $this->requestStack $requestStack;
  12.     }
  13.     public static function getSubscribedEvents()
  14.     {
  15.         return [
  16.             AuthenticationSuccessEvent::class => 'onAuthenticationSuccess',
  17.         ];
  18.     }
  19.     public function onAuthenticationSuccess(AuthenticationSuccessEvent $event)
  20.     {
  21.         // get user's lang setting and save it in the session, so LocaleListener can get it
  22.         $user $event->getAuthenticationToken()->getUser();
  23.         $session $this->requestStack->getSession();
  24.         $session->set('user_lang'$user->getLang());
  25.     }
  26. }