Tim Fountain
Tim Fountain

Reputation: 33148

Adding route as child of route from another module

In ZF2, how can I add a route as a child of a route from another module? I'm assuming there's a way to hook into a event somewhere?

E.g. Module A defines a route for /foo. In Module B, I would like to add a route /foo/bar, by creating a /bar route as a child of the 'foo' one.

Upvotes: 1

Views: 461

Answers (1)

Crisp
Crisp

Reputation: 11447

I was going to try to explain, but maybe an example will be better

ModuleA

supplies a /parent route which has a child route of /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',
                          ),
                      ),
                 ),
             ),
         ),
     ),
 ),

Module B

adds a child route of /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',
                          ),
                      ),
                 ),
             ),
         ),
     ),
 ),

The route definition in ModuleB will be merged with ModuleA's when your application loads module configurations, and you'll end up with /foo and /bar as children of /parent, with both pointing to their respective module controllers.

Upvotes: 5

Related Questions