router = $router; } 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 listener KernelEvents::EXCEPTION => ['onKernelException', 2], ]; } public function onKernelException(ExceptionEvent $event): void { $exception = $event->getThrowable(); if (!$exception instanceof AccessDeniedException && !$exception instanceof NotFoundHttpException) { return; } $response = new RedirectResponse($this->router->generate('app_home')); // optionally set the custom response $event->setResponse($response); } }