Last active
February 5, 2016 10:42
-
-
Save jbinfo/5edf5253ba32b019c739 to your computer and use it in GitHub Desktop.
Symfony: Right way to get default target URL from session http://goo.gl/SNq3Tr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace AppBundle\Security\Authentication\Handler; | |
use Symfony\Component\HttpFoundation\RedirectResponse; | |
use Symfony\Component\DependencyInjection\ContainerInterface; | |
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; | |
use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface; | |
class LoginSuccessHandler implements AuthenticationSuccessHandlerInterface | |
{ | |
private $container; | |
private $session; | |
private $router; | |
private $tokenStorage; | |
public function __construct(ContainerInterface $container) | |
{ | |
$this->container = $container; | |
$this->session = $container->get('session'); | |
$this->router = $container->get('router'); | |
if ($this->container->has('security.token_storage')) { | |
$this->tokenStorage = $this->container->get('security.token_storage'); | |
} else { | |
$this->tokenStorage = $this->container->get('security.context'); | |
} | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function onAuthenticationSuccess(TokenInterface $token) | |
{ | |
// .... | |
$defaultTargetPath = sprintf('_security.%s.target_path', $this->tokenStorage->getToken()->getProviderKey()); | |
if ($this->session->has($defaultTargetPath)) { | |
$url = $this->session->get($defaultTargetPath); | |
} | |
// .... | |
return new RedirectResponse($url); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment