I want to redirect the users :
If the user is not French or English, the default redirection is : www.website.com/en/
For the moment, I have created a LocaleListener
:
Dim\MxBundle\Listener\LocaleListener.php:
class LocaleListener implements EventSubscriberInterface
{
private $defaultLocale;
public function __construct($defaultLocale, $availableLocales, $container)
{
$this->defaultLocale = $defaultLocale;
$this->availableLocales = $availableLocales;
$this->container= $container;
}
public function onKernelRequest(GetResponseEvent $event)
{
$request = $event->getRequest();
if (!$request->hasPreviousSession())
{
$locale = $event->getRequest()->getPreferredLanguage($this->availableLocales);
$event->getRequest()->setLocale($locale);
$Session = $this->container->get('session');
$Session->set('_locale', $locale);
return;
}
# try to see if the locale has been set as a _locale routing parameter
if ($locale = $event->getRequest()->getPreferredLanguage($this->availableLocales)) {
$request->getSession()->set('_locale', $locale);
} else {
# if no explicit locale has been set on this request, use one from the session
$request->setLocale($request->getSession()->get('_locale', $this->defaultLocale));
}
}
public static function getSubscribedEvents()
{
return array(
// must be registered before the default Locale listener
KernelEvents::REQUEST => array(array('onKernelRequest', 17)),
);
}
}
Symfony/app/config/routing.yml:
dim_mx:
resource: "@DimMxBundle/Resources/config/routing.yml"
prefix: /
dim_mx:
resource: "@DimMxBundle/Resources/config/routing.yml"
prefix: /{_locale}/
requirements:
_locale: en|fr
root:
pattern: /
defaults:
_controller: FrameworkBundle:Redirect:urlRedirect
path: /%locale%/
permanent: true
To summarize, I want to redirect the user to www.website.com/en/ if an English user or www.website.com/fr/ if a French user. And for the moment, all users are redirected to /en/.
Thanks you all for your help.
To resolve my problem, I have installed the jmsI18nRoutingBundle Bundle.
http://jmsyst.com/bundles/JMSI18nRoutingBundle
Thanks you all for your time. Best regards, Dimitri
To redirect someone with php you can use the function void header ( string $string [, bool $replace = true [, int $http_response_code ]] )
you only have to know from where they come and then you can redirect them for fr userer like
header('Location: http://www.website.com/fr/');
for more look at php.net