Routing 如何定义路由并将其映射到Zend Framework 2应用程序模块的module.config.php中的操作?

Routing 如何定义路由并将其映射到Zend Framework 2应用程序模块的module.config.php中的操作?,routing,action,zend-framework2,Routing,Action,Zend Framework2,我的应用程序有一个模块目录,应该显示三个视图: URI为/catalog/ 当URI为/catalog/%city%时,%city%中的体育项目列表 当URI为/catalog/%city%/%sport%时,%city%中%sport%的课程列表 我的路由选项当前如下所示: <?php return array( ... 'router' => array( 'routes' => array( 'catalog'

我的应用程序有一个模块
目录
,应该显示三个视图:

  • URI为
    /catalog/
  • 当URI为
    /catalog/%city%
    时,
    %city%
    中的体育项目列表
  • 当URI为
    /catalog/%city%/%sport%
    时,
    %city%
    %sport%
    的课程列表
我的路由选项当前如下所示:

<?php
return array(
    ...
    'router' => array(
        'routes' => array(
            'catalog' => array(
                'type'    => 'segment',
                'options' => array(
                    'route'    => '/catalog[/:city][/:sport][/]',
                    'constraints' => array(
                        'city'  => '[a-zA-Z][a-zA-Z0-9_-]*',
                        'sport' => '[a-zA-Z][a-zA-Z0-9_-]*',
                    ),
                    'defaults' => array(
                        'controller' => 'Catalog\Controller\Catalog',
                        'action'     => 'index',
                    ),
                ),
            ),
        ),
    ),
    ...
);
如何定义路由,使其映射到上述操作

Thx

有效

'router' => array(
    'routes' => array(
        'catalog' => array(
            'type'    => 'literal',
            'options' => array(
                'route'    => '/catalog',
                'defaults' => array(
                    'controller' => 'Catalog\Controller\Catalog',
                    'action'     => 'index',
                ),
            ),
            'may_terminate' => true,
            'child_routes' => array(
                'city' => array(
                    'type'    => 'segment',
                    'options' => array(
                        'route'    => '/:city',
                        'constraints' => array(
                            'city'  => '[a-zA-Z][a-zA-Z0-9_-]*',
                            'sport' => '[a-zA-Z][a-zA-Z0-9_-]*',
                        ),
                        'defaults' => array(
                            'controller' => 'Catalog\Controller\Catalog',
                            'action'     => 'list-sports',
                        ),
                    ),
                    'may_terminate' => true,
                    'child_routes' => array(
                        'sport' => array(
                            'type'    => 'segment',
                            'options' => array(
                                'route'    => '/:sport',
                                'constraints' => array(
                                    'city'  => '[a-zA-Z][a-zA-Z0-9_-]*',
                                    'sport' => '[a-zA-Z][a-zA-Z0-9_-]*',
                                ),
                                'defaults' => array(
                                    'controller' => 'Catalog\Controller\Catalog',
                                    'action'     => 'list-courses',
                                ),
                            ),
                        ),
                    ),
                ),
            ),
        ),
    ),
),

好的,我想,我已经找到了答案:我需要一条
listCitiesAction()
的路线,一条
child\u路线
listSportsAction()
路线,还有一条
child\u路线
listSportsAction()
。我想你的意思是:“您可以使用子路由将路由单独映射到不同的操作”当我问这个问题时,我对
child_-route
选项一无所知。糟糕,我没有意识到您是问这个问题的人:P
'router' => array(
    'routes' => array(
        'catalog' => array(
            'type'    => 'literal',
            'options' => array(
                'route'    => '/catalog',
                'defaults' => array(
                    'controller' => 'Catalog\Controller\Catalog',
                    'action'     => 'index',
                ),
            ),
            'may_terminate' => true,
            'child_routes' => array(
                'city' => array(
                    'type'    => 'segment',
                    'options' => array(
                        'route'    => '/:city',
                        'constraints' => array(
                            'city'  => '[a-zA-Z][a-zA-Z0-9_-]*',
                            'sport' => '[a-zA-Z][a-zA-Z0-9_-]*',
                        ),
                        'defaults' => array(
                            'controller' => 'Catalog\Controller\Catalog',
                            'action'     => 'list-sports',
                        ),
                    ),
                    'may_terminate' => true,
                    'child_routes' => array(
                        'sport' => array(
                            'type'    => 'segment',
                            'options' => array(
                                'route'    => '/:sport',
                                'constraints' => array(
                                    'city'  => '[a-zA-Z][a-zA-Z0-9_-]*',
                                    'sport' => '[a-zA-Z][a-zA-Z0-9_-]*',
                                ),
                                'defaults' => array(
                                    'controller' => 'Catalog\Controller\Catalog',
                                    'action'     => 'list-courses',
                                ),
                            ),
                        ),
                    ),
                ),
            ),
        ),
    ),
),