src/EventSubscriber/RedirectAfterLoginSubscriber.php line 26

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use Symfony\Component\Security\Http\SecurityEvents;
  4. use Symfony\Component\HttpFoundation\RedirectResponse;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
  7. class RedirectAfterLoginSubscriber implements EventSubscriberInterface
  8. {
  9.     public static function getSubscribedEvents()
  10.     {
  11.         return [
  12.             SecurityEvents::INTERACTIVE_LOGIN => 'onLoginSuccess',
  13.         ];
  14.     }
  15.     /**
  16.      * Redirect user after successful login
  17.      * 
  18.      * @param InteractiveLoginEvent $event
  19.      * 
  20.      * @return [type]
  21.      */
  22.     public function onLoginSuccess(InteractiveLoginEvent $event)
  23.     {
  24.         $targetPath $event->getRequest()->get('_target_path');
  25.         if ($targetPath !== null && $targetPath !== '') {
  26.             $response = new RedirectResponse($targetPath302);
  27.             $response->send();
  28.         }
  29.     }
  30. }