ZendFramework2模块之间的路由


ZendFramework2 routing between modules

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

可以。

但是现在我想使用第二个模块,我想设置一条从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. php:

<a href="<?php echo $this->url('sporttabs',array('module' => 'sporttabs','controller' => 'sporttab','action' => 'index'))?>">Sporttabs-Projekt</a>

这个不行,我只能得到/sporttab

即使我尝试做www.myurl.de/sporttabs我不进入sporttabs模块…(我使用ZendStudio来生成新的模块,所以我认为所有的文件都在正确的位置…)

你能给我提示一下怎么做吗?

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

这是我的/admin路由的一个例子,这是一个名为Admin的不同模块,位于Application旁边。

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

/default的存在是为了访问子路由

@Stanimir找到了我的解决方案的正确提示:

在为我的应用编辑路由时,我一定是无意中改变了路由数组的顺序:'may_terminate'和'child_routes'部分被移到了更高的位置,而不是'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张贴在他的回答工作得很好,我进入我的新模块…

谢谢你的帮助Stanimir!