src/EventListener/RequestListener.php line 27

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use App\Entity\Redirection;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use Symfony\Component\HttpFoundation\RedirectResponse;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Symfony\Component\HttpKernel\Event\RequestEvent;
  8. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  9. use Symfony\Component\Routing\Router;
  10. use Symfony\Component\Routing\RouterInterface;
  11. use Symfony\Component\VarDumper\VarDumper;
  12. class RequestListener
  13. {
  14.     private $em;
  15.     private $router;
  16.     
  17.     public function __construct(RouterInterface $router,EntityManagerInterface $em)
  18.     {
  19.         $this->router $router;
  20.         $this->em $em;
  21.     }
  22.     
  23.     public function onKernelRequest(RequestEvent $event)
  24.     {
  25.         
  26.         if ($event->getRequest()->getRequestUri()) {
  27.             if ($event->getThrowable() instanceof NotFoundHttpException) {
  28.                 $code "404";
  29.             } else {
  30.                 $code "500";
  31.             }
  32.             $redirections $this->em->getRepository("App:Redirection")->findBy(['path' => $event->getRequest()->getRequestUri(), 'deletedAt' => null]);
  33.             
  34.             if (count($redirections)<=0) {
  35.                 $target null;
  36.                 foreach ($redirections as $redirection){
  37.                     if ($redirection->getTarget()){
  38.                         $target $redirection->getTarget();
  39.                     }
  40.                 }
  41. //                if($target !== null){
  42. //                    $response = new RedirectResponse($redirection->getTarget(), Response::HTTP_MOVED_PERMANENTLY);
  43. //                    $event->setResponse($response);
  44. //                }else{
  45. //                    $event->setResponse(new RedirectResponse($this->router->generate('error_controller_index_test', ['code'=>$code])));
  46. //                    $event->stopPropagation();
  47. //                }
  48.             }
  49.         }
  50.     }
  51. }