src/EventSubscriber/UserLocaleSubscriber.php line 20

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\User;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  6. use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
  7. use Symfony\Component\Security\Http\SecurityEvents;
  8. class UserLocaleSubscriber implements EventSubscriberInterface
  9. {
  10.     private SessionInterface $session;
  11.     public function __construct(SessionInterface $session)
  12.     {
  13.         $this->session $session;
  14.     }
  15.     public function onInteractiveLogin(InteractiveLoginEvent $event): void
  16.     {
  17.         /** @var User $user */
  18.         $user $event->getAuthenticationToken()->getUser();
  19.         $this->session->set('_locale'$user->getLocale());
  20.     }
  21.     public static function getSubscribedEvents()
  22.     {
  23.         return [
  24.             SecurityEvents::INTERACTIVE_LOGIN => 'onInteractiveLogin',
  25.         ];
  26.     }
  27. }