1

I am trying to use same route names for the 2 different modules, is it possible?

Module User :

 /*Module.config.php*/

 'dashboard' => array(
                'type'    => 'segment',
                'options' => array(
                    'route'    => '/dashboard',
                    'constraints' => array(
                        'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                    ),
                    'defaults' => array(
                        'controller' => 'Users\Controller\Users',
                        'action'     => 'dashboard',
                    ),
                ),
 ),

Module Admin :

/*Module.config.php*/ 

'dashboard' => array(
                'type'    => 'segment',
                'options' => array(
                    'route'    => '/dashboard',
                    'constraints' => array(
                        'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                    ),
                    'defaults' => array(
                        'controller' => 'Admin\Controller\Admin',
                        'action'     => 'dashboard',
                    ),
                ),
  ),

Eventhough I am creating 2 different modules for the dashboard, I am loading only any one action.

How can I achieve this ?

blackbishop
  • 30,945
  • 11
  • 55
  • 76
mark winkle
  • 206
  • 1
  • 3
  • 17

1 Answers1

6

I think you can not have the same name for two different routes. Yes it's two different modules, but it's the same application.

The reason is that when the Zend\ModuleManager loads the modules, the event ModuleEvent::EVENT_LOAD_MODULE will be triggered and then the listener Zend\ModuleManager\Listener\ConfigListener will call the function getConfig() of each single Module in your application. And then, all the Module->getConfig() will be merged into one internal configuration called application.config.

This is to say that when the modules are loaded, you'll have two routes with th same name, and the difference between the modules doesn't affect nothing in the routing.

Even if could do that, you'll encounter other problems as when you want to use Redirect Plugin, for example the toRoute method need the route name as parameter :

toRoute(string $route = null, array $params = array(), array $options = array(), boolean $reuseMatchedParams = false)

This is a problem if you have to call it with the same route name.

A possible solution for your problem is to set one route and add the module to it, as follows :

/dashboard/admin/the-rest-of-the-url

/dashboard/user/the-rest-of-the-url

You'll have something like this in your route configuration :

'dashboard' => array( 
'type'    => 'segment', 
'options' => array( 
    'route'    => '/dashboard[/:module][/:controller][/:action][/:id]', 
    'constraints' => array( 
        'module'       => '[a-zA-Z][a-zA-Z0-9_-]*', 
        'controller' => '[a-zA-Z][a-zA-Z0-9_-]*', 
        'action'     => '[a-zA-Z][a-zA-Z0-9_-]*', 
        'id'         => '[0-9]+', 
    ), 
    'defaults' => array( 
        'controller' => 'Application', 
        'action'     => 'index',
    ), 
), 
'may_terminate' => true, 
'child_routes' => array( 
    'default' => array( 
        'type'    => 'Wildcard', 
        'options' => array( 
        ), 
    ), 
), 
), 
blackbishop
  • 30,945
  • 11
  • 55
  • 76