Skip to content

Instantly share code, notes, and snippets.

@jbinfo
Last active February 5, 2016 10:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jbinfo/5edf5253ba32b019c739 to your computer and use it in GitHub Desktop.
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
<?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