src/EventListener/RequestListener.php line 38

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use App\Entity\Redirection;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Symfony\Component\HttpFoundation\RedirectResponse;
  7. use Symfony\Component\HttpFoundation\Response;
  8. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  9. use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
  10. use Symfony\Component\HttpKernel\Event\RequestEvent;
  11. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  12. use Symfony\Component\Routing\Router;
  13. use Symfony\Component\Routing\RouterInterface;
  14. use Symfony\Component\VarDumper\VarDumper;
  15. class RequestListener implements EventSubscriberInterface
  16. {
  17.     private $em;
  18.     private $router;
  19.     public function __construct(RouterInterface $routerEntityManagerInterface $em)
  20.     {
  21.         $this->router $router;
  22.         $this->em $em;
  23.     }
  24.     public static function getSubscribedEvents(): array
  25.     {
  26.         return [
  27.             ExceptionEvent::class => 'onException',
  28.             RequestEvent::class => 'onRequest',
  29.         ];
  30.     }
  31.     public function onException(RequestEvent $event)
  32.     {
  33.         if ($event->getRequest()->getRequestUri()) {
  34.             if ($event->getThrowable() instanceof NotFoundHttpException) {
  35.                 $code "404";
  36.             } else {
  37.                 $code "500";
  38.             }
  39.             $redirections $this->em->getRepository("App:Redirection")->findBy(['path' => $event->getRequest()->getRequestUri(), 'deletedAt' => null]);
  40.             if (count($redirections) <= 0) {
  41.                 $redirection = new Redirection();
  42.                 $redirection->setPath($event->getRequest()->getRequestUri());
  43.                 $redirection->setCode($code);
  44.                 $this->em->persist($redirection);
  45.                 $this->em->flush();
  46.             } else {
  47.                 $target null;
  48.                 foreach ($redirections as $redirection) {
  49.                     if ($redirection->getTarget()) {
  50.                         $target $redirection->getTarget();
  51.                     }
  52.                 }
  53.                 if ($target !== null) {
  54.                     $response = new RedirectResponse($redirection->getTarget(), Response::HTTP_MOVED_PERMANENTLY);
  55.                     $event->setResponse($response);
  56.                 }
  57.             }
  58.         }
  59.     }
  60.     public function onRequest(RequestEvent $event)
  61.     {
  62.         // do not redirect if the request is for an API endpoint or the admin panel
  63.         if (str_starts_with($event->getRequest()->getRequestUri(), '/api') || str_starts_with($event->getRequest()->getRequestUri(), '/admin')) {
  64.             return;
  65.         }
  66.         if ($event->isMainRequest()) {
  67.             $session $event->getRequest()->getSession();
  68.             if($event->getRequest()->get('selected_locale')){
  69.                 $session->set('selected_locale'$event->getRequest()->get('selected_locale'));
  70.             }
  71.             $browserLocale $event->getRequest()->getPreferredLanguage(array('en''fr'));
  72.             if (!$session->has('selected_locale') && $event->getRequest()->getLocale() != $browserLocale) {
  73.                 $route $this->router->match($event->getRequest()->getPathInfo());
  74.                 if(array_key_exists('_route'$route)){
  75.                     $attributes $route;
  76.                     unset($attributes['_route']);
  77.                     unset($attributes['_controller']);
  78.                     $attributes['_locale'] = $browserLocale;
  79.                     $response = new RedirectResponse($this->router->generate($route["_route"], $attributes));
  80.                     $event->getRequest()->setLocale($browserLocale);
  81.                     $event->setResponse($response);
  82.                 }
  83.             }
  84.         }
  85.     }
  86. }