src/EventListener/ExceptionListener.php line 26

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use Symfony\Component\HttpFoundation\Response;
  4. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  5. use Symfony\Component\Security\Core\Exception\AccessDeniedException;
  6. use Symfony\Component\Security\Core\Exception\InsufficientAuthenticationException;
  7. use Twig\Environment;
  8. /**
  9.  * Class ExceptionListener
  10.  */
  11. class ExceptionListener
  12. {
  13.     protected Environment $templating;
  14.     /**
  15.      * ExceptionListener constructor.
  16.      */
  17.     public function __construct(Environment $templating)
  18.     {
  19.         $this->templating $templating;
  20.     }
  21.     public function onKernelException(ExceptionEvent $event): void
  22.     {
  23.         $exception $event->getThrowable();
  24.         if (
  25.             $exception instanceof InsufficientAuthenticationException
  26.             || $exception instanceof AccessDeniedException
  27.         ) {
  28.             $response = new Response();
  29.             $response->setContent($this->templating->render('errors/404.html.twig'));
  30.             $response->setStatusCode(Response::HTTP_NOT_FOUND);
  31.             $event->setResponse($response);
  32.         }
  33.     }
  34. }