src/Controller/Frontend/CountryController.php line 28

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Frontend;
  3. use App\Controller\Controller;
  4. use App\Twig\Extension\CountryExtension;
  5. use Symfony\Component\HttpFoundation\Cookie;
  6. use Symfony\Component\HttpFoundation\JsonResponse;
  7. use Symfony\Component\HttpFoundation\RedirectResponse;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpFoundation\Response;
  10. class CountryController extends Controller
  11. {
  12.     // 3600×24×7 = 604800 seconds
  13.     public const ONE_WEEK 604800;
  14.     public const COOKIE_NAME 'dont-change-country';
  15.     public function dontChangeCountryAction(): Response
  16.     {
  17.         $response = new JsonResponse(['result' => 1]);
  18.         $response->headers->setCookie(Cookie::create(self::COOKIE_NAME1time() + self::ONE_WEEK));
  19.         return $response;
  20.     }
  21.     public function redirectToCountry(Request $requestCountryExtension $countryExtension)
  22.     {
  23.         $scheme $request->getScheme();
  24.         $country $request->get('country''fr');
  25.         $domain $countryExtension->getCountryDomain($country);
  26.         $route $request->get('route''homepage');
  27.         $params $request->get('params', []);
  28.         // switch language in order to avoid having a language prefix
  29.         $params['_locale'] = $countryExtension->getCountryDefaultLocale($country);
  30.         $url $scheme.'://'.$domain.$countryExtension->getRouteForCountry($country$route$params);
  31.         return new RedirectResponse($urlResponse::HTTP_PERMANENTLY_REDIRECT);
  32.     }
  33. }