【问题标题】:Zend Framework 2: Using multiple routes with multiple modulesZend Framework 2:使用多个路由和多个模块
【发布时间】:2014-04-03 12:40:00
【问题描述】:

我在我的 zend 框架 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
  • URL /moduleB 路由到 /ModuleA/Index/Index

预期:

  • URL /moduleA 路由到 /ModuleA/Index/Index
  • URL /moduleB 路由到 /ModuleB/Index/Index

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

【问题讨论】:

  • 您的路由配置中似乎存在某种歧义。我们将需要查看两个模块的实际路线以了解它是什么。
  • 路由应该有唯一的名字。如果一个模块应该覆盖另一个模块的配置,一个常见的最佳实践是只通过更改模块加载顺序来做到这一点。摆弄优先级可能会导致一些非常不受欢迎的连锁效应。

标签: php configuration module routing zend-framework2


【解决方案1】:

你是否也在你的 moduleB 配置中使用'controller' =&gt; 'index',

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

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

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

【讨论】:

    【解决方案2】:

    只需添加 'priority' 参数

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-15
      • 2017-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-04
      相关资源
      最近更新 更多