vendor/zenstruck/redirect-bundle/src/EventListener/RedirectOnNotFoundListener.php line 21

Open in your IDE?
  1. <?php
  2. namespace Zenstruck\RedirectBundle\EventListener;
  3. use Symfony\Component\HttpFoundation\RedirectResponse;
  4. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  5. use Zenstruck\RedirectBundle\Service\RedirectManager;
  6. /**
  7.  * @author Kevin Bond <kevinbond@gmail.com>
  8.  */
  9. class RedirectOnNotFoundListener extends NotFoundListener
  10. {
  11.     private $redirectManager;
  12.     public function __construct(RedirectManager $redirectManager)
  13.     {
  14.         $this->redirectManager $redirectManager;
  15.     }
  16.     public function onKernelException(ExceptionEvent $event)
  17.     {
  18.         if (!$this->isNotFoundException($event)) {
  19.             return;
  20.         }
  21.         $redirect $this->redirectManager->findAndUpdate($event->getRequest()->getPathInfo());
  22.         if (null === $redirect) {
  23.             return;
  24.         }
  25.         $event->setResponse(new RedirectResponse(
  26.             $redirect->getDestination(),
  27.             $redirect->isPermanent() ? 301 302
  28.         ));
  29.     }
  30. }