2013-05-22 95 views
4

我的應用程序翻譯我想用一個語言結構,例如:路由Zend框架2語言的網址

  • site.com(英文)
  • site.com/de/(德國)
  • site.com/fr/(法國)
  • site.com/nl/(荷蘭)

等。

我可以使用Literal中的路由器選項輕鬆完成此操作,但我希望排除我不支持的語言,例如, site.com/it/如果它不被支持我想要一個404。我試圖用正則表達式(添加支持的語言)來解決這個問題,但是某些東西(我不知道)出錯了。

在此先感謝!

'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%', 
         ), 
        ), 
       ), 
      ), 
     ), 
    ), 

回答

5

我覺得你的正則表達式必須

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

,你可以這麼用段路線和適當的約束一樣...

'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)?', 
         ), 
        ), 
       ), 
      ), 
     ), 
    ), 
), 
+0

的感謝!我拿了你的第二個選擇:) – directory