php zend framework 2 routing with dynamic controller name but same controller

这一生的挚爱 提交于 2020-01-16 00:45:42

问题


i'm running into a routing beginners problem with zend framework 2: i want to make a routing that will work something like this:

www.mysite.com/city/school/class

with the routing i want to be able:

www.mysite.com/chicago

will take me to a city.phtml page with "chicago" as a parameter

same with

www.mysite.com/chicago/jcc

will take me to a school.phtml with "jcc" as a parameter name

and so on..

what i tried to do is:

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

but i don't have any idea how to continue from here :(

thanks!


回答1:


after a couple of hours i got it working, hope this helps someone:

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

so when calling

www.mysite.com/chicago 

i am redirected to the city action and can get the chicago var in my controller by:

$this->params()->fromRoute('city')



来源:https://stackoverflow.com/questions/20771663/php-zend-framework-2-routing-with-dynamic-controller-name-but-same-controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!