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

时间:2013-03-19 20:20:52

标签: routing action zend-framework2

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

  • 城市列表,当URI为/catalog/
  • %city%
  • 时,/catalog/%city%中的体育列表
  • %sport%
  • 时,%city%/catalog/%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',
                    ),
                ),
            ),
        ),
    ),
    ...
);

我的CatalogController

class CatalogController extends AbstractActionController {

    public function indexAction() {
        return new ViewModel();
    }

    public function listCitiesAction() {} // all cities

    public function listSportsAction() {} // all sports for the city

    public function listCoursesAction() {} // all courses for the sport in the city
}

如何定义路线,以便它映射到上面的动作?

THX

1 个答案:

答案 0 :(得分:0)

有效!

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