src/EventListener/SwitchUserListener.php line 12

Open in your IDE?
  1. <?php
  2. // src/AppBundle/EventListener/SwitchUserListener.php
  3. namespace App\EventListener;
  4. use Symfony\Component\Security\Http\Event\SwitchUserEvent;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Symfony\Component\Security\Http\SecurityEvents;
  7. class SwitchUserListener implements EventSubscriberInterface
  8. {
  9.     public function onSwitchUser(SwitchUserEvent $event)
  10.     {
  11.         $event->getRequest()->getSession()->set(
  12.             '_locale',
  13.             // assuming your User has some getLocale() method
  14.             $event->getTargetUser()->getLocale()
  15.         );
  16.     }
  17.     public static function getSubscribedEvents(): array
  18.     {
  19.         return array(
  20.             // constant for security.switch_user
  21.             SecurityEvents::SWITCH_USER => 'onSwitchUser',
  22.         );
  23.     }
  24. }