<?php
namespace App\Controller;
use Monolog\Logger;
use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Response;
class EntryController extends AbstractController
{
/**
* @Route("/", name="root")
*
* the user comes from 'https://www.quintas-das-corgas.fr/'.
* redirects to home page with language parameter
*
* @param Request the symfony request object
*
* @return Response object
*
*/
public function index(Request $request)
{
$language = $request->getLocale();
$router = $this->get('router');
$route = 'home.' . $language;
$url = $this->container->get('router')->generate($route);
return new RedirectResponse($url);
}
/**
* @Route("/setlocale/{language}", name="setlocale")
*
* {{ app.request.locale }}
* $this->getRequest()->getLocale()
*
* @return Response
*/
public function setLocale(Request $request, $language)
{
$url = $request->headers->get('referer');
$language_list = array('fr', 'en', 'pt');
if(!in_array($language, $language_list))
{
$language = 'en'; // default language
}
if(empty($url))
{
$route = 'home.' . $language; // $request->getLocale();
$url = $this->container->get('router')->generate($route);
}
else // if($url)
{
$url_path = parse_url($url, PHP_URL_PATH);
$router = $this->get('router');
$route = $router->match($url_path)['_route'];
$route .= "." . $language;
return $this->redirectToRoute($route, ["_locale" => $language]);
}
return new RedirectResponse($url);
}
}