src/EventListener/ExceptionEventListener.php line 27

Open in your IDE?
  1. <?php
  2. /**
  3.  * @author <akartis-dev>
  4.  */
  5. namespace App\EventListener;
  6. use ApiPlatform\Core\Api\UrlGeneratorInterface;
  7. use Symfony\Component\HttpFoundation\RedirectResponse;
  8. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  9. use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
  10. class ExceptionEventListener
  11. {
  12.     public function __construct(
  13.         private UrlGeneratorInterface $urlGenerator,
  14.     )
  15.     {
  16.     }
  17.     /**
  18.      * Handle user access denied
  19.      *
  20.      * @param ExceptionEvent $event
  21.      * @return void
  22.      */
  23.     public function onKernelException(ExceptionEvent $event)
  24.     {
  25.         $indexPath $this->urlGenerator->generate('app_index', ['_locale' => 'fr']);
  26.         if ($event->getThrowable() instanceof AccessDeniedHttpException) {
  27.             $event->setResponse(new RedirectResponse($indexPath));
  28.         }
  29.     }
  30. }