2012-05-21 174 views
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