src/Controller/Front/HomeController.php line 17

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Front;
  3. use App\Repository\Configuration\SurveyRepository;
  4. use App\Repository\LanguagesRepository;
  5. use App\Service\LanguagesManager;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\HttpFoundation\Response;
  8. use Symfony\Component\Routing\Annotation\Route;
  9. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  10. #[Route('/')]
  11. class HomeController extends AbstractController
  12. {
  13.     #[Route('/'name'front_home')]   
  14.     public function index(Request $request
  15.                          SurveyRepository $surveyRepository,
  16.                          LanguagesManager $languagesManager): Response
  17.     {
  18.         return $this->render('front/home.html.twig', [
  19.             'surveys' => $surveyRepository->getAllQb(
  20.                 [
  21.                     'fk' => ['survey.label'],
  22.                     'fk_description' => ['survey.description'],
  23.                     'fk_media' => ['survey.vignette_form_request'],
  24.                     'id_language' => $languagesManager->getLanguageUser($request),
  25.                     'IsEnable' => 1,
  26.                     'order_by_position' => true
  27.                 ]
  28.             )->getResult()
  29.         ]);
  30.     }
  31.     #[Route('/change_locale'name:'change_locale')]
  32.     public function changeLocale(Request $requestLanguagesRepository $languagesRepository$lang ="fr")
  33.     {
  34.         $old_locale $request->getLocale();
  35.         $id $request->request->get('id');
  36.         if($id$selectLanguage $languagesRepository->findOneById($id); 
  37.         else $selectLanguage $languagesRepository->findOneByCode($lang);
  38.         if($selectLanguage) {
  39.             $targetLocale $selectLanguage->getCode();
  40.             $request->setLocale($targetLocale);
  41.             $request->getSession()->set('_locale'$targetLocale);
  42.         }
  43.   
  44.         $referer $request->headers->get('referer');
  45.         if($old_locale != "[a-z]{2}"$referer str_replace('/'.$old_locale'/'.$targetLocale$referer);
  46.         else {
  47.             $referer str_replace('/'.$request->getDefaultLocale(), '/'.$targetLocale$referer);
  48.         }
  49.   
  50.         return new Response($referer);
  51.     }
  52. }