Domanda

I use two modules in my zend framework 2 application:

  • Module A
  • Module B

I encounter the problem that I can only use one of the routes that I have configured for the corresponding module. The route that is used, depends on the sorting of the modules in the application.config.php file:

<?php
return array(
    'modules' => array(
        'ModuleA','ModuleB'
    );
?>

Every module contains nearly the same configuration module.config.php:

<?php
return array(
    'router' => array(
        'routes' => array(
            'ModuleA' => array(
                'type'    => 'Literal',
                'options' => array(
                    'route'    => '/moduleA',
                    'defaults' => array(
                        '__NAMESPACE__' => 'ModuleA\Controller',
                        'controller'    => 'index',
                        'action'        => 'index',
                        ),
                    ),
                    'may_terminate' => false,
                    'child_routes' => array(
                        'moduleA-index' => array(
                            'type' => 'Segment',
                            'options' => array(
                                'route' => '/index[/:action]',
                                'defaults' => array(
                                'controller' => 'index',
                                'action' => 'index'
                            )
                        )
                    )
                )
            )
        )
    )
);

Current situation:

  • URL /moduleA routes to /ModuleA/Index/Index
  • URL /moduleB routes to /ModuleA/Index/Index

Expected:

  • URL /moduleA routes to /ModuleA/Index/Index
  • URL /moduleB routes to /ModuleB/Index/Index

Do you have any advice for me how to use both configurations/routes in the right way?

È stato utile?

Soluzione

Are you using 'controller' => 'index', in your moduleB config too?

if yes then there is your problem index is on alias and only 1 controller can have that alias, in other words alias's should be unique throw out the Application and not just a module.

define a unique name(alias) for your controller and you will be fine.

in my project i just use the FQN so there is no confusion (Namespace\Controller\ControllerName)

Altri suggerimenti

Just add 'priority' param

<?php
return array(
    'router' => array(
        'routes' => array(
            'ModuleA' => array(
                'type'    => 'Literal',
                'priority' => 100,            // <-- priority
                'options' => array(
                    'route'    => '/moduleA',
                    'defaults' => array(
                        '__NAMESPACE__' => 'ModuleA\Controller',
                        'controller'    => 'index',
                        'action'        => 'index',
                        ),
                    ),
                    'may_terminate' => false,
                    'child_routes' => array(
                        'moduleA-index' => array(
                            'type' => 'Segment',
                            'options' => array(
                                'route' => '/index[/:action]',
                                'defaults' => array(
                                'controller' => 'index',
                                'action' => 'index'
                            )
                        )
                    )
                )
            )
        )
    )
);
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top