Tim*_*ain 1 routing zend-framework2
在ZF2中,如何从另一个模块添加路由作为路由的子节点?我假设有办法在某个地方挂钩活动?
例如,模块A定义了一条路线/foo.在模块B中,我想/foo/bar通过创建/bar路径作为'foo'的子路径来添加路由.
我打算尝试解释,但也许一个例子会更好
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',
),
),
),
),
),
),
),
Run Code Online (Sandbox Code Playgroud)
模块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',
),
),
),
),
),
),
),
Run Code Online (Sandbox Code Playgroud)
当您的应用程序加载模块配置时,ModuleB中的路由定义将与ModuleA合并,并且您将最终使用/ foo和/ bar作为/ parent的子项,两者都指向其各自的模块控制器.