[英]ZF2 dynamic routes
I need to build dynamic multi level routing in zf2, but not sure what way to approach it. 我需要在zf2中构建动态多级路由,但不确定采用哪种方法。 What i need is:
我需要的是:
/listing[/:directory1[/:directory2[/dir...]] as unlimited nesting to mimic a directory structure. / listing [/:directory1 [/:directory2 [/ dir ...]]作为无限嵌套来模仿目录结构。
Ideally all directories should come either as an array or at least a single variable like "directory1/directory2/directory3...". 理想情况下,所有目录都应以数组形式出现,或者至少应使用单个变量,例如“ directory1 / directory2 / directory3 ...”。
i can not find anywhere ho to properly set up these parameters or even if zf2 supports that kind of routing. 我找不到任何可以正确设置这些参数的位置,即使zf2支持这种路由也是如此。 My current route config looks like this:
我当前的路由配置如下所示:
...
'gallery' => array(
'type' => 'Literal',
'options' => array(
'route' => '/Gallery',
'defaults' => array(
'__NAMESPACE__' => 'Application\Controller',
'controller' => 'Gallery',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
'listing' => array(
'type' => 'Segment',
'options' => array(
'route' => '/listing[/:directory1[/:directory2]]',
'constraints' => array(
'directory1' => '[a-zA-Z][a-zA-Z0-9_-]*',
'directory2' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
'__NAMESPACE__' => 'Application\Controller',
'controller' => 'Listing',
'action' => 'index',
),
),
),
),
),
...
Two possible approaches: 两种可能的方法:
Here is the solution using regex route: 这是使用正则表达式路由的解决方案:
...
'listing' => array(
'type' => 'Segment',
'options' => array(
'route' => '/listing',
'constraints' => array(
'user' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
'__NAMESPACE__' => 'Application\Controller',
'controller' => 'Listing',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
'directory' => array(
'type' =>'regex',
'options' => array(
'regex' => '/(?<dirPath>.*)',
'defaults' => array(
'__NAMESPACE__' => 'Application\Controller',
'controller' => 'Listing',
'action' => 'index',
),
'spec' => '/%dirPath%',
),
),
...
inside controller $directory = $this->params()->fromRoute('dirPath'); 内部控制器$ directory = $ this-> params()-> fromRoute('dirPath'); returns a url string that can be parsed.
返回可以解析的网址字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.