src/Exception/Handler/NotFoundExceptionHandler.php line 20
<?php
// src/Exception/Handler/NotFoundExceptionHandler.php
namespace App\Exception\Handler;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class NotFoundExceptionHandler implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return [
'kernel.exception' => 'onKernelException',
];
}
public function onKernelException(ExceptionEvent $event)
{
$exception = $event->getThrowable();
if ($exception instanceof NotFoundHttpException) {
// Rediriger vers la page d'erreur personnalisée
$response = new Response('La page demandée n\'existe pas.', 404);
$event->setResponse($response);
}
}
}