Search code examples
urlzend-frameworkhttp-redirectrouteslocale

redirect root url according to locale in Zend


Working in ZF: Is it possible to change the base or root url on the www.example.com to www.example.com/$language/home, thus depending on the (browser) locale of the users browser?

Example; If a guest manually types www.example.com I would like to change the url automatically to a url with locale: www.example.com/en/home for guests in en_GB region or www.example.fr/home for guests in fr_FR region.

From the root-url I have all the menu-urls and content locale aware. Clicking a link to a menu item in the url the lang is automatically added after the root. The content on the root url is locale aware too using translate, so english for en_GB en french for fr_FR, etc.

Still missing though I would like to have the root url change to locale aware right from the start of the visit to the application if only the root is entered.

I guess something like root :to => redirect("/prepayments") in Rails 3 from what I understand from this Q&A on this forum

I have tried and implemented controller action helpers, redirects, etc. etc. for as far as I could find on this forum, but they all don't offer the solution.
A redirect in htaccess is not possible I think since I first need to get the locale from the browser. Since I don't know this the redirect is dynamic and I cannot set a redirect in htaccess.

I woud appreciate any suggestions?


Solution

  • I finally found the next solution on my question with a little help from Florent in Zend Framework: Redirect from action helper:

    In my Language Plugin I added

    public function routeShutdown(Zend_Controller_Request_Abstract $request)
    {...some code handling translate...
    
    $url = $request->getRequestUri();
    
    if ($url === '/') {
        $redirector = Zend_Controller_Action_HelperBroker::getStaticHelper('Redirector');
        $redirector->gotoUrl($lang.'/home');
    }
    

    The routes I have them in a routes.ini so $lang.'/home' is routed to the indexController and indexAction of the Default module.