src/EventSubscriber/NotFoundSubscriber.php line 19

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  4. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  5. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Twig\Environment;
  8. class NotFoundSubscriber implements EventSubscriberInterface
  9. {
  10.     private $twig;
  11.     public function __construct(Environment $twig)
  12.     {
  13.         $this->twig $twig;
  14.     }
  15.     public function onKernelException(ExceptionEvent $event)
  16.     {
  17.         $exception $event->getThrowable();
  18. /*
  19.         if ($exception instanceof NotFoundHttpException) {
  20.             // Ici, tu peux personnaliser selon le chemin
  21.             $response = new Response(
  22.                 $this->twig->render('error/entity_not_found.html.twig', [
  23.                     'type' => null,
  24.                     'id'   => null,
  25.                 ]),
  26.                 404
  27.             );
  28.             $event->setResponse($response);
  29.         }
  30. */
  31.     }
  32.     public static function getSubscribedEvents()
  33.     {
  34.         return [
  35.             'kernel.exception' => 'onKernelException',
  36.         ];
  37.     }
  38. }