To change this, we need to implement a new routing rule and then replace the default route with it. The
Front Controller will then be able to do its magic and ensure that the correct controller and action are called.
This is done in the bootstrap part of the code as shown in listing 14.8.
Listing 14.8: Implementing a new routing rule for language support
$languages = array_keys($config->languages->toArray()); A
$zl = new Zend_Locale();
$lang = in_array($zl->getLanguage(), $languages) B
? $zl->getLanguage() : 'en'; B
// add language to default route
$route = new Zend_Controller_Router_Route( 1
':lang/:controller/:action/*', 1
array('controller'=>'index', 1
'action' => 'index', 1
'module'=>'default', 1
'lang'=>$lang)); 1
$router = $frontController->getRouter(); C
$router->addRoute('default', $route); C
$frontController->setRouter($router); C
A Load the list of allowed language codes from the config
B Use the browser??™s language code if it is in the allowed list
1 Create new route
C Update the router with the new route
Licensed to Menshu You
Zend Framework in Action (Ch01) Manning Publications Co. 42
We use Zend_Controller_Router_Route to define the route (#1) using the : character to define the variable
parts with language first, then controller, then action and then the asterisk meaning ???all other parameters???.
Pages:
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355