Created
December 16, 2017 16:21
-
-
Save Crazal/46d511201a230ff516a6020a64f3d6fa to your computer and use it in GitHub Desktop.
Redirect from exception
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
services: | |
kernel.listener.redirectexception: | |
class: AppBundle\EventListener\RedirectExceptionListener | |
arguments: | |
- "@router" | |
tags: | |
- { name: kernel.event_listener, event: kernel.exception } |
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
{{ render(controller('AppBundle:Default:login')) }} |
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\Controller; | |
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; | |
use Symfony\Bundle\FrameworkBundle\Controller\Controller; | |
use Symfony\Component\HttpFoundation\Request; | |
use AppBundle\Exception\RedirectException; | |
class DefaultController extends Controller | |
{ | |
/** | |
* @Route("/", name="homepage") | |
*/ | |
public function indexAction(Request $request) | |
{ | |
return $this->render('default/index.html.twig', [ | |
'base_dir' => realpath($this->getParameter('kernel.project_dir')).DIRECTORY_SEPARATOR, | |
]); | |
} | |
/** | |
* @Route("/login", name="login") | |
*/ | |
public function loginAction(Request $request) | |
{ | |
if ($this->getUser()) { | |
throw new RedirectException('This user does not have access to this section.'); | |
} | |
return $this->render('default/login.html.twig', []); | |
} | |
/** | |
* @Route("/login_form", name="login_form") | |
*/ | |
public function loginFormAction(Request $request) | |
{ | |
return $this->render('default/login_form.html.twig', []); | |
} | |
} |
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\EventListener; | |
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent; | |
use Symfony\Component\HttpFoundation\RedirectResponse; | |
use AppBundle\Exception\RedirectException; | |
use Symfony\Component\Routing\RouterInterface; | |
class RedirectExceptionListener | |
{ | |
private $router; | |
public function __construct(RouterInterface $router) | |
{ | |
$this->router = $router; | |
} | |
public function onKernelException(GetResponseForExceptionEvent $event) | |
{ | |
if ((!$event->getException()->getPrevious() instanceof RedirectException) | |
&& (!$event->getException() instanceof RedirectException)) { | |
return; | |
} | |
$response = new RedirectResponse($this->router->generate('homepage')); | |
$event->setResponse($response); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment