Php ZendFramework2模块之间的路由

Php ZendFramework2模块之间的路由,php,zend-framework2,zend-route,Php,Zend Framework2,Zend Route,我安装了一个SkeletonApplication,并在标准模块“Application”中实现了一些控制器 那很好 但是现在我想使用第二个模块,我想设置从“应用程序”模块到新模块的路由,以便在视图中将其链接到新模块 第二个模块名为“Sporttabs” 在我的application.config.php中,我在文档中设置了: // This should be an array of module namespaces used in the application. 'modules' =

我安装了一个SkeletonApplication,并在标准模块“Application”中实现了一些控制器

那很好

但是现在我想使用第二个模块,我想设置从“应用程序”模块到新模块的路由,以便在视图中将其链接到新模块

第二个模块名为“Sporttabs”

在我的application.config.php中,我在文档中设置了:

 // This should be an array of module namespaces used in the application.
'modules' => array(
    'Application',
    'Sporttabs'

),
在module.config.php中设置的模块“Application”中:

'routes' => array(
        'home' => array(
            'type' => 'Zend\Mvc\Router\Http\Literal',
            'options' => array(
                'route'    => '/',
                'defaults' => array(
                    'module'    => 'Application',
                    'controller' => 'Index',
                    'action'     => 'index',
                ),
            ),
        ),

        'fach' => array(
            'type'    => 'segment',
            'options' => array(
                'route'    => '/index[/:action][/:id]',
                'constraints' => array(
                    'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
                    'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                    'id'     => '[0-9]+',
                ),
                'defaults' => array(
                    'controller' => 'Index',
                    'action'     => 'index'
                ),
            ),
        ),

        'sporttabs' => array(
            'type'    => 'segment',
            'options' => array(
                'route'    => '/sporttabs[/:controller][/:action][/:id]',
                'constraints' => array(
                    'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
                    'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                    'id'     => '[0-9]+',
                ),
                'defaults' => array(
                    'module' => 'Sporttabs',
                    'controller' => 'Sporttab',
                    'action'     => 'index'
                ),
            ),
        ),



    ),

),
我尝试在index.phtml中链接它:

<a href="<?php echo $this->url('sporttabs',array('module' => 'sporttabs','controller' => 'sporttab','action' => 'index'))?>">Sporttabs-Projekt</a>
这不起作用,我只得到/sporttab

即使我尝试访问www.myurl.de/sporttabs,我也不会进入sporttabs模块。。。 我正在使用ZendStudio生成ne模块,所以我认为所有文件都位于正确的位置


你能告诉我怎么做吗?

不需要在应用程序配置中定义sporttabs。我建议您在Sporttabs的module.config.php文件中执行此操作

这是my/admin路由的一个示例,它是一个名为admin的不同模块,位于应用程序旁边

'router' => [
        'routes' => [
            'admin' => [
                'type'    => 'Literal',
                'options' => [
                    'route' => '/admin',
                    'defaults' => [
                        '__NAMESPACE__' => 'Admin\Controller',
                        'controller'    => 'Index',
                        'action'        => 'index',
                    ],
                ],
                'may_terminate' => true,
                'child_routes' => [
                    'default' => [
                        'type'    => 'Segment',
                        'options' => [
                            'route'    => '/[:controller[/][:action[/][:id][/page/:page][/search/:search]]]',
                            'constraints' => [
                                'controller' => '[a-zA-Z0-9_-]*',
                                'action'     => '[a-zA-Z0-9_-]*',
                                'search'     => '[a-zA-Z0-9_-]*',
                                'id'         => '[0-9]+',
                                'page'       => '[0-9]+',
                            ],
                            'defaults' => [
                                '__NAMESPACE__' => 'Admin\Controller',
                                'controller'    => 'Index',
                                'action'        => 'index',
                            ],
                        ],
                    ],
                ],
            ],
        ],
    ],
从那里我可以做到:

<?=$this->url("admin/default", ['controller' => "controler_name", "action" => "action_name_from_controller", "id" => id_or_something_else_if_needed])?>

/默认设置是为了访问子路由。

@Stanimir为我的解决方案点击了正确的提示:

在编辑应用程序的路由时,我必须无意中更改路由数组的顺序: “可终止”和“子路由”部分移到更高层,而不是“fach”路由的一部分

因此,我对数组进行了如下更改:

'routes'=> array(

        'fach'=> array(
            'type'      => 'Literal',
            'options'   => array(
                'route'     => '/',
                'defaults'  => array(
                    '__NAMESPACE__' => 'Application\Controller',
                    'controller'    => 'Index',
                    'action'        => 'index',
                ),
            ),


        'may_terminate' => 'true',
        'child_routes'  => array(
            'default'   => array(
                'type'  => 'Segment',
                'options'   => array(
                    'route' => '/[:controller[/][:action[/][:id]]]',
                    'constraints'   => array(
                        'controller'    => '[a-zA-Z][a-zA-Z0-9_-]*',
                        'action'        => '[a-zA-Z][a-zA-Z0-9_-]*',
                        'id'            => '[0-9]+',
                    ),
                    'defaults'  => array(
                        '__NAMESPACE__' => 'Application\Controller',
                        'controller'    => 'Index',
                        'action'        => 'index',
                    ),
                ),
            ),
        ),
      ),),
在路由中包含名称空间后,我还必须更改控制器数组,因为控制器的别名从“Index”更改为“Application\controller\Index”:

'controllers' => array(
    'invokables' => array(
        'Application\Controller\Index' => 'Application\Controller\IndexController',

    ),
),
同样的错误阻止我进入第二个模块,路由数组也被错误排序

现在Stanimir在回答中发布的链接解决方案运行良好,我进入了我的新模块


谢谢你的帮助,斯坦尼米尔

通配符路由在skeleton应用程序中用作一种魔术,因此新用户不必定义每条路由。我建议您尝试简化您的约束,并拥有比这更明确的路由,尤其是了解路由是如何工作的。