从另一个模块添加路由作为路由的子路径

时间:2013-05-16 09:59:22

标签: routing zend-framework2

在ZF2中,如何从另一个模块添加路由作为路由的子节点?我假设有办法在某个地方挂钩活动?

E.g。模块A定义/foo的路由。在模块B中,我想通过创建/foo/bar路由作为'foo'的子路径来添加路由/bar

1 个答案:

答案 0 :(得分:5)

我打算尝试解释,但也许一个例子会更好

ModuleA

提供/parent路线,其路线为/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/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的子节点,两者都指向它们各自的模块控制器。

相关问题