1

I want to redirect the users :

  • English users : www.website.com/en/
  • French users : www.website.com/fr/

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.

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
Dimitri
  • 922
  • 2
  • 13
  • 34
  • Take a look at the symfony2 documentation http://symfony.com/doc/current/book/routing.html or at the JMS I18N routing bundle http://jmsyst.com/bundles/JMSI18nRoutingBundle/master/configuration – sebbo Jun 05 '14 at 12:45
  • 1
    There is similar question here: http://stackoverflow.com/questions/19484027/translations-in-symfony-2-3-locale-in-request and http://stackoverflow.com/questions/14903963/symfony2-locale-languages-whole-page-event-listener – Diablo Jun 05 '14 at 12:49

2 Answers2

0

To resolve my problem, I have installed the jmsI18nRoutingBundle Bundle.

http://jmsyst.com/bundles/JMSI18nRoutingBundle

Thanks you all for your time. Best regards, Dimitri

Dimitri
  • 922
  • 2
  • 13
  • 34
-1

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

Baalthasarr
  • 377
  • 1
  • 13