Zend Framework 2:使用具有多个模块的多个路由

时间:2014-03-02 16:34:24

标签: php configuration module routing zend-framework2

我在zend framework 2应用程序中使用了两个模块:

  • 模块A
  • 模块B

我遇到的问题是我只能使用一个我为相应模块配置的路由。使用的路由取决于 application.config.php 文件中模块的排序:

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

每个模块包含几乎相同的配置 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'
                            )
                        )
                    )
                )
            )
        )
    )
);

现状:

  • URL / moduleA路由到/ ModuleA / Index / Index
  • 网址/模块B路由到/ ModuleA /索引/索引

预期:

  • URL / moduleA路由到/ ModuleA / Index / Index
  • 网址/模块B路由到/ ModuleB /索引/索引

对于我如何以正确的方式使用这两种配置/路线,您有什么建议吗?

2 个答案:

答案 0 :(得分:0)

只需添加'优先级'参数

<?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'
                            )
                        )
                    )
                )
            )
        )
    )
);

答案 1 :(得分:0)

您是否也在moduleB配置中使用'controller' => 'index',

如果是,那么您的问题index在别名上,只有 1 控制器可以拥有该别名,换句话说,别名应该是唯一的抛弃应用程序而不仅仅是一个模块

为您的控制器定义一个唯一的名称(别名),你会没事的。

在我的项目中我只使用FQN,所以没有混淆(Namespace\Controller\ControllerName

相关问题