<?php
namespace App\EventSubscriber;
use Symfony\Component\Security\Http\SecurityEvents;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
class RedirectAfterLoginSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return [
SecurityEvents::INTERACTIVE_LOGIN => 'onLoginSuccess',
];
}
/**
* Redirect user after successful login
*
* @param InteractiveLoginEvent $event
*
* @return [type]
*/
public function onLoginSuccess(InteractiveLoginEvent $event)
{
$targetPath = $event->getRequest()->get('_target_path');
if ($targetPath !== null && $targetPath !== '') {
$response = new RedirectResponse($targetPath, 302);
$response->send();
}
}
}