2013-05-16 84 views
1

在ZF2,我怎麼能添加路由從另一個模塊的路線的孩子?我假設有一種方法可以將某個事件綁定到某個事件上?添加路線爲路線的孩子從另一個模塊

E.g.模塊A定義了/foo的路由。在模塊B,我想補充的路線/foo/bar,通過創建一個/bar路線爲「富」一個的孩子。

+0

除了由@Crisp提供的答案(http://samminds.com/2013/04/understanding-zf2-configuration/)[關於「理解ZF2配置」我的博客文章]可能是你的興趣。實質上,所有模塊配置都將合併爲一個大配置。有了這些知識,清脆的答案可能是比較容易理解的,因爲最終你只是用一個陣列工作);-) – Sam

回答

5

我會試着解釋,但也許一個例子會更好

ModuleA

提供其具有的/parent/foo孩子航線

// routes 
'router' => array(
    'routes' => array(
     'parent' => array(
      'type' => 'Literal', 
      'may_terminate' => true, 
      'options' => array(
       'route' => '/parent', 
       'defaults' => array(
        '__NAMESPACE__' => __NAMESPACE__ . '\Controller', 
        'controller' => 'Index', 
        'action'  => 'index', 
       ), 
      ), 
      'child_routes' => array(
       'foo' => array(
        'type' => 'Literal', 
        'options' => array(
          'route' => '/foo' 
          'defaults' => array(
           '__NAMESPACE__' => 'ModuleA\Controller', 
           'controller' => 'Foo', 
           'action'  => 'index', 
         ), 
        ), 
       ), 
      ), 
     ), 
    ), 
), 

模塊B

一個 /parent路線

增加了/parent/bar

小孩路線
// routes 
'router' => array(
    'routes' => array(
     'parent' => array(
      'child_routes' => array(
       'bar' => array(
        'type' => 'Literal', 
        'options' => array(
          'route' => '/bar' 
          'defaults' => array(
           '__NAMESPACE__' => 'ModuleB\Controller', 
           'controller' => 'Bar', 
           'action'  => 'index', 
         ), 
        ), 
       ), 
      ), 
     ), 
    ), 
), 

當您的應用程序加載模塊配置時,ModuleB中的路由定義將與ModuleA合併,並且最終將/ foo和/ bar作爲/ parent的子節點,同時指向它們各自的模塊控制器。

相關問題