php zend framework 2 routing with dynamic controll

2019-08-11 09:54发布

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条回答
冷血范
2楼-- · 2019-08-11 10:18

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

查看更多
登录 后发表回答