0

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

  • 城市列表,当 URI 为/catalog/
  • 中的运动列表%city%,当 URI 为/catalog/%city%
  • %sport%中的课程列表%city%,当 URI 为/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
}

如何定义路线,使其映射到上述操作?

谢谢

4

1 回答 1

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',
                                ),
                            ),
                        ),
                    ),
                ),
            ),
        ),
    ),
),
于 2013-03-19T22:20:21.600 回答