【问题标题】:zend framework 2 - issue in using same route name in two diffrent moduleszend framework 2 - 在两个不同的模块中使用相同的路由名称的问题
【发布时间】:2015-04-08 14:46:16
【问题描述】:

我正在尝试为 2 个不同的模块使用相同的路由名称,这可能吗?

模块用户

 /*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.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',
                    ),
                ),
  ),

尽管我为仪表板创建了 2 个不同的模块,但我只加载了任何一个操作。

我怎样才能做到这一点?

【问题讨论】:

  • 你想要发生什么?您说您正在尝试使用两个不同的路由名称,但您的示例中的两个路由具有相同的名称,因此一个会覆盖另一个。
  • 对不起,蒂姆,它是“相同的路线名称”

标签: php zend-framework2 zend-route zend-router


【解决方案1】:

我认为您不能为两条不同的路线使用相同的名称。是的,这是两个不同的模块,但它是同一个应用程序。

原因是Zend\ModuleManager加载模块时,会触发ModuleEvent::EVENT_LOAD_MODULE事件,然后listenerZend\ModuleManager\Listener\ConfigListener 将调用应用程序中每个模块的函数getConfig()。然后,所有Module->getConfig() 将合并到一个名为application.config 的内部配置中。

也就是说,当模块被加载时,你会有两条同名的路由,模块之间的区别不会影响routing中的任何内容。

即使可以这样做,当你想使用Redirect Plugin 时,你也会遇到其他问题,例如toRoute 方法需要路由名称作为参数:

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

如果您必须使用相同的路由名称调用它,这是一个问题。

您的问题的一个可能解决方案是设置一个路由并将模块添加到其中,如下所示:

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

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

你的路由配置中会有这样的东西:

'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( 
        ), 
    ), 
), 
), 

【讨论】:

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