src/EventListener/AccessDeniedListener.php line 33
<?php// src/EventListener/AccessDeniedListener.phpnamespace App\EventListener;use Symfony\Component\EventDispatcher\EventSubscriberInterface;use Symfony\Component\HttpFoundation\RedirectResponse;use Symfony\Component\HttpFoundation\Response;use Symfony\Component\HttpKernel\Event\ExceptionEvent;use Symfony\Component\HttpKernel\KernelEvents;use Symfony\Component\Routing\Generator\UrlGeneratorInterface;use Symfony\Component\Security\Core\Exception\AccessDeniedException;use Symfony\Component\HttpFoundation\Session\Flash\FlashBag;class AccessDeniedListener implements EventSubscriberInterface{private $urlGenerator;public function __construct(UrlGeneratorInterface $urlGenerator){$this->urlGenerator = $urlGenerator;}public static function getSubscribedEvents(): array{return [// the priority must be greater than the Security HTTP// ExceptionListener, to make sure it's called before// the default exception listenerKernelEvents::EXCEPTION => ['onKernelException', 2],];}public function onKernelException(ExceptionEvent $event): void{$exception = $event->getThrowable();if (!$exception instanceof AccessDeniedException) {return;}// ... perform some action (e.g. logging)// optionally set the custom response//Fixme -- flash message is not working but redirect is fine.$flashMessage = new FlashBag();$flashMessage->set('notice', "You don't have permission to access this page." );$response = new RedirectResponse($this->urlGenerator->generate('app_home'));$event->setResponse($response);//$event->setResponse(new Response(null, 403));// or stop propagation (prevents the next exception listeners from being called)//$event->stopPropagation();}}