0

我正在創建一個使用ZF2的多語言應用程序..並且無法確定如何添加將構成每個URL的基礎的部分URL,而不管模塊。Zend Framework 2 - Zend Mvc Router Http Part - 模塊配置

http://localhost/en/us/application/index/index/ 

我完全理解了如何使用DI

http://localhost/application/index/index/ 
http://localhost/guestbook/index/index/ 
http://localhost/forum/index/index/ 

我不明白的是如何配置部分航線,這將是對所有的路線基本配置/[:namespace[/:controller[/:action]]]。在ZF1我使用路線鏈接實現這個..

所以我需要配置的一部分路線其廣泛應用的網站,然後讓模塊配置/[:namespace[/:controller[/:action]]]或任何其他必要的途徑..

http://localhost/en/us/application/index/index/ 
http://localhost/zh/cn/application/index/index/ 
http://localhost/en/uk/forum/index/index/ 

回答

2

我想你正在尋找的是child_routes配置項。看看如何ZfcUser configures it's routing (here):它創建一個基礎Literal路由(/用戶),然後通過child_routes數組鏈接子路線(/用戶/登錄等)到它。

我覺得這樣的事情會做的伎倆爲您提供:

'router' => array(
    'routes' => array(
     'myapp' => array(
      'type' => 'Segment', 
      'options' => array(
       'route' => '/[:lang[/:locale]]', 
       'defaults' => array(
        'lang' => 'en', 
        'locale' => 'us', 
       ), 
      ), 
      'may_terminate' => false, 
      'child_routes' => array(
       'default' => array(
        'type' => 'Segment', 
        'options' => array(
         'route' => '/[:controller[/:action]]', 
         'constraints' => array(
          'controller' => '[a-zA-Z][a-zA-Z0-9_-]*', 
          'action'  => '[a-zA-Z][a-zA-Z0-9_-]*', 
        ), 
        'defaults' => array(
         'controller' => 'index', 
         'action'  => 'index', 
        ), 
       ), 
      ), 
     ), 
    ), 
), 

然後在您的控制器,你可以做到這一點得到了郎和語言環境:

$this->params()->fromRoute('lang'); 
$this->params()->fromRoute('locale'); 
+0

感謝您的回答..我正在尋找..是它設置lang:locale路由在我的默認應用程序..然後它將成爲所有其他模塊的基地.. ie ..如果我使用ZfcUser ..該URL然後將/ zh/cn/user或/ zh/cn/user/login .. – chameleon95

+0

child_r outes跨越模塊路由工作..我試圖按照路線合併,但它不工作,因爲我想.. – chameleon95