src/Listener/ExceptionListener.php line 35

Open in your IDE?
  1. <?php
  2. /**
  3.  * This file is part of the pdAdmin package.
  4.  *
  5.  * @package     pd-admin
  6.  * @license     LICENSE
  7.  * @author      Ramazan APAYDIN <apaydin541@gmail.com>
  8.  * @link        https://github.com/appaydin/pd-admin
  9.  */
  10. namespace App\Listener;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  14. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  15. use Symfony\Component\HttpKernel\KernelEvents;
  16. use Twig\Environment;
  17. /**
  18.  * Exception Listener.
  19.  *
  20.  * @author Ramazan APAYDIN <apaydin541@gmail.com>
  21.  */
  22. class ExceptionListener implements EventSubscriberInterface
  23. {
  24.     private Environment $engine;
  25.     public function __construct(Environment $engine)
  26.     {
  27.         $this->engine $engine;
  28.     }
  29.     public function onKernelException(ExceptionEvent $event): void
  30.     {
  31.         // Get Exception
  32.         $exception $event->getThrowable();
  33.         if ($exception instanceof NotFoundHttpException) {
  34.             $event->setResponse(new Response($this->engine->render('admin/layout/404.html.twig'), 404));
  35.         }
  36.     }
  37.     public static function getSubscribedEvents(): array
  38.     {
  39.         return [KernelEvents::EXCEPTION => [['onKernelException']]];
  40.     }
  41. }