Zend Framework 2 Routing Language in URL

For my translation of the application, I would like to use the language structure, for example:

  • site.com (English)
  • site.com/de/(German)
  • site.com/fr/(france)
  • site.com/nl/( Dutch)

etc..

I can do this easily with the router parameter in Literal as "[az] {2}", but I want to exclude languages ​​that I don't support, for example. site.com/it/, if it is not supported, I want 404. I tried to fix it with a regular expression (adding supported languages), but something (I don’t know) went wrong.

Thanks in advance!

'router' => array( 'routes' => array( 'home' => array( 'type' => 'Literal', 'options' => array( 'route' => '/', 'defaults' => array( 'controller' => 'Application\Controller\Index', 'action' => 'index', ), ), 'may_terminate' => true, 'child_routes' => array( 'language' => array( 'type' => 'Regex', 'options' => array( 'regex' => '/(?<lang>(de|fr|nl))?', 'defaults' => array( 'lang' => 'en', //default ), 'spec' => '/%lang%', ), ), ), ), ), ), 
+4
source share
1 answer

I think your regular expression should be

 'regex' => '/(?<lang>(de|fr|nl)?)' 

You could also use the Segment route and the corresponding restriction ...

 'router' => array( 'routes' => array( 'home' => array( 'type' => 'Literal', 'options' => array( 'route' => '/', 'defaults' => array( 'controller' => 'Application\Controller\Index', 'action' => 'index', ), ), 'may_terminate' => true, 'child_routes' => array( 'language' => array( 'type' => 'Segment', 'options' => array( 'route' => '[/:lang]', 'defaults' => array( 'lang' => 'en', //default ), 'constraints' => array( 'lang' => '(en|de|fr|nl)?', ), ), ), ), ), ), ), 
+5
source

Source: https://habr.com/ru/post/1482155/


All Articles