[EXCEPTION][UI][UX] Add RedirectException, which can be thrown anywhere to redirect somewhere, and an exception handler

This commit is contained in:
Hugo Sales
2020-09-06 21:38:37 +00:00
committed by Hugo Sales
parent 420b4767b2
commit b906dde059
5 changed files with 74 additions and 21 deletions

View File

@@ -30,11 +30,14 @@
namespace App\Core;
use App\Util\Exception\RedirectException;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\ControllerEvent;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
use Symfony\Component\HttpKernel\Event\ViewEvent;
use Symfony\Component\HttpKernel\KernelEvents;
@@ -95,10 +98,23 @@ class Controller extends AbstractController implements EventSubscriberInterface
return $event;
}
public function onKernelException(ExceptionEvent $event)
{
if (($except = $event->getThrowable()) instanceof RedirectException) {
if (($redir = $except->redirect_response) != null) {
$event->setResponse($redir);
} else {
$event->setResponse(new RedirectResponse($event->getRequest()->getPathInfo()));
}
}
return $event;
}
public static function getSubscribedEvents()
{
return [
KernelEvents::CONTROLLER => 'onKernelController',
KernelEvents::EXCEPTION => 'onKernelException',
KernelEvents::VIEW => 'onKernelView',
];
}