src/Controller/EntryController.php line 26

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Monolog\Logger;
  4. use Psr\Log\LoggerInterface;
  5. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  6. use Symfony\Component\Routing\Annotation\Route;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\RedirectResponse;
  9. use Symfony\Component\HttpFoundation\Response;
  10. class EntryController extends AbstractController
  11. {
  12.     /**
  13.      * @Route("/", name="root")
  14.      * 
  15.      * the user comes from 'https://www.quintas-das-corgas.fr/'.
  16.      * redirects to home page with language parameter
  17.      * 
  18.      * @param Request the symfony request object
  19.      * 
  20.      * @return Response object
  21.      * 
  22.      */
  23.     public function index(Request $request)
  24.     {
  25.         $language $request->getLocale();
  26.         $router $this->get('router');
  27.         $route 'home.' $language;
  28.         $url $this->container->get('router')->generate($route);
  29.         
  30.         return new RedirectResponse($url);
  31.     }
  32.     /**
  33.      * @Route("/setlocale/{language}", name="setlocale")
  34.      * 
  35.      * {{ app.request.locale }}
  36.      * $this->getRequest()->getLocale()
  37.      *
  38.      * @return Response
  39.      */
  40.     public function setLocale(Request $request$language)
  41.     {
  42.       $url $request->headers->get('referer');
  43.       $language_list = array('fr''en''pt');
  44.       if(!in_array($language$language_list))
  45.       {
  46.         $language 'en'// default language
  47.       }
  48.       if(empty($url))
  49.       {
  50.         $route 'home.' $language// $request->getLocale();
  51.         $url $this->container->get('router')->generate($route);
  52.       }
  53.       else // if($url)
  54.       {
  55.         $url_path parse_url($urlPHP_URL_PATH);
  56.         $router $this->get('router');
  57.         $route $router->match($url_path)['_route'];
  58.         $route .= "." $language;
  59.         return $this->redirectToRoute($route, ["_locale" => $language]);
  60.       }
  61.       
  62.       return new RedirectResponse($url);
  63.     }
  64. }