php zend框架2路由与动态控制器名称但相同的控制器

时间:2013-12-25 10:20:54

标签: php routing zend-framework2

我遇到了zend框架2的路由初学者问题: 我想制作一个类似于这样的路由:

  

www.mysite.com/city/school/class

使用我想要的路由:

  

www.mysite.com/chicago

将带我到带有“芝加哥”作为参数的city.phtml页面

相同
  

www.mysite.com/chicago/jcc

将带我到带有“jcc”作为参数名称的school.phtml

依旧......

我试图做的是:

return array(
'router' => array(
    'routes' => array(
        'main' => array(
            'type'    => 'segment',
            'options' => array(
                'route'    => '[/:city][/:school][/:class]',
                'constraints' => array(
                    'city'      => '[a-zA-Z][a-zA-Z0-9_-]*',
                    'school'    => '[a-zA-Z][a-zA-Z0-9_-]*',
                    'class'     => '[a-zA-Z][a-zA-Z0-9_-]*',
                ),

                'defaults' => array(
                    '__NAMESPACE__' => 'Application\Controller',
                    'controller'    => 'Index',
                    'action'        => 'index',
                ),
            ),
        ...

但我不知道如何从这里继续:(

谢谢!

1 个答案:

答案 0 :(得分:1)

经过几个小时我开始工作,希望这有助于某人:

'router' => array(
    'routes' => array(
        'city' => array(
            'type'    => 'Segment',
            'options' => array(
                'route'    => '/main[/][:city]',
                'constraints' => array(
                    'city'     => '[a-zA-Z][a-zA-Z0-9_-]*',
                ),
                'defaults' => array(
                    '__NAMESPACE__' => 'Main\Controller',
                    'controller'    => 'main',
                    'action'        => 'city',
                ),
            ),
            'may_terminate' => true,
            'child_routes' => array(
                'school' => array(
                    'type'    => 'Segment',
                    'options' => array(
                        'route'    => '[/][:school]',
                        'constraints' => array(
                            'school' => '[a-zA-Z][a-zA-Z0-9_-]*',
                        ),
                        'defaults' => array(
                            '__NAMESPACE__' => 'Main\Controller',
                            'controller'    => 'main',
                            'action'        => 'school',
                        ),
                    ),
                    'may_terminate' => true,
                    'child_routes' => array(
                        'class' => array(
                            'type'    => 'Segment',
                            'options' => array(
                                'route'    => '[/][:class]',
                                'constraints' => array(
                                    'class' => '[a-zA-Z][a-zA-Z0-9_-]*',
                                ),
                                'defaults' => array(
                                    '__NAMESPACE__' => 'Main\Controller',
                                    'controller'    => 'main',
                                    'action'        => 'class',
                                ),
                            ),
                        ),
                    ),
                ),
            ),
        ),
    ),
),

所以在打电话时

www.mysite.com/chicago 

我被重定向到城市行动,可以通过以下方式获取我的控制器中的芝加哥变种:

  

$这 - > PARAMS() - > fromRoute( '城市')