src/Controller/FrontController.php line 107

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Language;
  4. use App\Entity\SectionAcl;
  5. use App\Entity\Slug;
  6. use App\Service\RouteGenerator;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
  9. use Symfony\Component\Routing\Annotation\Route;
  10. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\Security\Core\Exception\AccessDeniedException;
  14. use Twig\Environment;
  15. class FrontController extends AbstractController
  16. {
  17.     /**
  18.      * @var EntityManagerInterface
  19.      */
  20.     private $em;
  21.     /**
  22.      * FrontController constructor.
  23.      * @param RouteGenerator $generator
  24.      * @param EntityManagerInterface $manager
  25.      */
  26.     public function __construct(EntityManagerInterface $manager, private Environment $twig)
  27.     {
  28.         $this->em $manager;
  29.     }
  30.     /**
  31.      * @Route("/", name="default")
  32.      *
  33.      * @return Response
  34.      */
  35.     public function redirectAction(Request $request): Response
  36.     {
  37.         if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) && (strpos(@$_SERVER['HTTP_REFERER'], $_SERVER['APP_DOMAIN']) === false)) {
  38.             // break up string into pieces (languages and q factors)
  39.             preg_match_all('/([a-z]{1,8}(-[a-z]{1,8})?)\s*(;\s*q\s*=\s*(1|0\.[0-9]+))?/i'$_SERVER['HTTP_ACCEPT_LANGUAGE'], $lang_parse);
  40.             if (count($lang_parse[1])) {
  41.                 // create a list like "en" => 0.8
  42.                 $langs array_combine($lang_parse[1], $lang_parse[4]);
  43.                 // set default to 1 for any without q factor
  44.                 foreach ($langs as $lang => $val) {
  45.                     if (strlen($lang) > 2) {
  46.                         unset($langs[$lang]);
  47.                         $lang substr($lang02);
  48.                     }
  49.                     if (empty($langs[$lang]) || !$langs[$lang])
  50.                         $langs[$lang] = 1;
  51.                 }
  52.                 // sort list based on value
  53.                 arsort($langsSORT_NUMERIC);
  54.                 $dbLangs = [];
  55.                 foreach ($this->em->getRepository(Language::class)->findAll() as $language) {
  56.                     $dbLangs[] = $language->getCode();
  57.                 }
  58.                 foreach (array_keys($langs) as $lang) {
  59.                     if (in_array($lang$dbLangs)) {
  60.                         return $this->redirectToRoute('security_home'array_merge(['_locale' => $lang], $request->query->all()));
  61.                     }
  62.                 }
  63.             }
  64.         }
  65.         return $this->redirectToRoute('security_home'$request->query->all());
  66.     }
  67. //    /**
  68. //     * This route ensures all GET requests (e.g. sections urls) end with a slash
  69. //     *
  70. //     * @Route("/{path}", name="add_trailing_slash", requirements={"path"="[^\/]$"}, methods={"GET"})
  71. //     *
  72. //     * @param Request $request
  73. //     * @return Response
  74. //     */
  75. //    public function addTrailingSlashAction(Request $request): Response
  76. //    {
  77. //        $pathInfo = $request->getPathInfo();
  78. //        $requestUri = $request->getRequestUri();
  79. //
  80. //        $url = str_replace($pathInfo, trim($pathInfo).'/', $requestUri);
  81. //
  82. //        return $this->redirect($url, 301);
  83. //    }
  84.     /**
  85.      * The main frontend route that ensures all the sections are displayed properly
  86.      * base on their Url
  87.      *
  88.      * @Route("{path}", name="front", requirements={"path"=".*"}, defaults={"path" = ""})
  89.      *
  90.      * @param Request $request
  91.      * @param Slug|null $slug
  92.      * @return Response
  93.      */
  94.     public function frontAction(): Response
  95.     {
  96.         return new Response($this->twig->render('404.html.twig'), 404);
  97.     }
  98. }