Phalcon php multilingual routing -


hi i'm new php world. i'm wondering best way handle multilingual routing ? i'm starting create website phalcon php. have following routing structure.

    $router->add('/{language:[a-z]{2}}/:controller/:action/:params', array(             'controller' => 2,             'action' => 3,             'params' => 4,     ));      $router->add('/{language:[a-z]{2}}/:controller/:action', array(             'controller' => 2,             'action' => 3,     ));      $router->add('/{language:[a-z]{2}}/:controller', array(             'controller' => 2,             'action' => 'index',     ));      $router->add('/{language:[a-z]{2}}', array(             'controller' => 'index',             'action' => 'index',     )); 

my problem instance when go on mywebsite.com/ want change url in dispatcher mywebsite.com/en/ or other language. practise handle in beforedispatchloop ? seek best solutions.

/**triggered before entering in dispatch loop.  * @ point dispatcher don't know if controller or actions executed exist.  * dispatcher knows information passed router.   * @param event $event  * @param dispatcher $dispatcher  */ public function beforedispatchloop(event $event, dispatcher $dispatcher) {         //$params = $dispatcher->getparams();         $params = array('language' => 'en');         $dispatcher->setparams($params);         return $dispatcher; } 

this code doesn't work @ all, url not change. url stay mywebsite.com/ , not mywebsite.com/en/ in advance.

i try 1 solution above. redirect doesn't seems work. try hard-coded test.

use phalcon\http\response;  //obtain standard eventsmanager di $eventsmanager = $di->getshared('eventsmanager'); $dispatcher = new phalcon\mvc\dispatcher();  $eventsmanager->attach("dispatch:beforedispatchloop",function($event, $dispatcher) {      $dispatcher->getdi->get('response')->redirect('/name/en/index/index/'); } 

i think confusing something. if understand correctly: want redirect users valid url if open page without specifying language.

if that's case should verify in event handler whether language parameter specified , redirect user same url + default language if missing. assuming beforedispatchloop located in controller, part of problem, because route without language never matches , never controller. instead need use event handler event manager per documentation. here how whole thing.

$di->get('eventsmanager')->attach("dispatch:beforedispatchloop", function(event $event, dispatcher $dispatcher) {     $params = $dispatcher->getparams();      // careful here, if matches valid route without language may go massive     // recursion. really, want use `beforeexecuteroute` event or explicitly     // check if `$_server['request_uri']` contains language part before redirecting here.      if (!isset($params['language']) {         $dispatcher->getdi->get('response')->redirect('/en' . $_server['request_uri']);         return false;     }      return $dispatcher; }); 

Comments