src/EventSubscriber/NotFoundHttpExceptionEventSubscriber.php line 13

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use Junker\JSendResponse\JSendFailResponse;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  6. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  7. use Symfony\Component\HttpKernel\KernelEvents;
  8. class NotFoundHttpExceptionEventSubscriber implements EventSubscriberInterface
  9. {
  10.     public function onKernelException(ExceptionEvent $event): void
  11.     {
  12.         $exception $event->getThrowable();
  13.         if (!($exception instanceof NotFoundHttpException)) {
  14.             return;
  15.         }
  16.         $event->setResponse(
  17.             new JSendFailResponse('Not found.'JSendFailResponse::HTTP_NOT_FOUND)
  18.         );
  19.     }
  20.     public static function getSubscribedEvents(): array
  21.     {
  22.         return [
  23.             KernelEvents::EXCEPTION => 'onKernelException',
  24.         ];
  25.     }
  26. }