BUGFIX: fixed error pages
This commit is contained in:
parent
7942801015
commit
74b238c3d5
@ -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
|
||||
|
@ -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;
|
||||
|
37
src/EventListener/ExceptionListener.php
Normal file
37
src/EventListener/ExceptionListener.php
Normal 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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user