As you can see from this cookbook page , the kernel.exception event is fired whenever an exception is thrown. I donโt know that there is a specific event for the NotFoundHttpException exception, but I would suggest creating my own listener service for all exceptions, and then check the type of exception inside the service and add your own logic.
(Note: I have not tested this, but it should at least give you an idea of โโhow this can be achieved.)
Configuration
acme.exception_listener: class: Acme\Bundle\AcmeBundle\Listener\RedirectExceptionListener arguments: [@doctrine.orm.entity_manager, @logger] tags: - { name: kernel.event_listener, event: kernel.exception, method: checkRedirect }
Listener Service
namespace Acme\Bundle\AcmeBundle\Listener; use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; use Symfony\Component\HttpKernel\Log\LoggerInterface; use Doctrine\ORM\EntityManager; class RedirectExceptionListener { protected $em; protected $logger; function __construct(EntityManager $em, LoggerInterface $logger) { $this->em = $em; $this->logger = $logger; } public function checkRedirect(GetResponseForExceptionEvent $event) { $exception = $event->getException(); if ($exception instanceof NotFoundHttpException) {
source share