BUGFIX: fixed error pages

This commit is contained in:
Jeroen De Meerleer 2022-04-29 13:51:06 +02:00
parent 7942801015
commit 74b238c3d5
Signed by: JeroenED
GPG Key ID: 28CCCB8F62BFADD6
3 changed files with 40 additions and 2 deletions

View File

@ -19,6 +19,9 @@ services:
- '../src/DependencyInjection/'
- '../src/Entity/'
- '../src/Kernel.php'
App\EventListener\ExceptionListener:
tags:
- { name: kernel.event_listener, event: kernel.exception }
# add more service definitions when explicit configuration is needed
# please note that last definitions always *replace* previous ones

View File

@ -4,8 +4,6 @@ namespace App\Controller;
use App\Repository\Page;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\KernelInterface;

View File

@ -0,0 +1,37 @@
<?php
namespace App\EventListener;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
class ExceptionListener
{
public function __construct(UrlGeneratorInterface $router)
{
$this->router = $router;
}
public function onKernelException(ExceptionEvent $event)
{
// You get the exception object from the received event
$exception = $event->getThrowable();
// HttpExceptionInterface is a special type of exception that
// holds status code and header details
if ($exception instanceof HttpExceptionInterface) {
$route = $this->router->generate('error', ['status' => $exception->getStatusCode()]);
} else {
$route = $this->router->generate('error', ['status' => Response::HTTP_INTERNAL_SERVER_ERROR]);
}
// Customize your response object to display the exception details
$response = new RedirectResponse($route);
// sends the modified response object to the event
$event->setResponse($response);
}
}