PHP Zend Framework 2使用动态控制器名称但具有相同控制器进行路由 [英] php zend framework 2 routing with dynamic controller name but same controller
问题描述
我在zend框架2中遇到了一个路由初学者的问题:
i想要创建一个可以像这样工作的路由:
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
www.mysite.com/city/school/class
我希望能够使用以下路由:
with the routing i want to be able:
www.mysite.com/chicago
www.mysite.com/chicago
将带我进入以芝加哥为参数的city.phtml页面
will take me to a city.phtml page with "chicago" as a parameter
与
www.mysite.com/chicago/jcc
www.mysite.com/chicago/jcc
会将我带到带有 jcc为参数名称
will take me to a school.phtml with "jcc" as a parameter name
等等。
我试图做的是:
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 :(
谢谢!
推荐答案
几个小时后,我开始工作,希望这对某人有帮助。
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',
),
),
),
),
),
),
),
),
),
所以在通话时
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')
$this->params()->fromRoute('city')
这篇关于PHP Zend Framework 2使用动态控制器名称但具有相同控制器进行路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!