ZF2动态路线
ZF2 dynamic routes
我需要在 zf2 中构建动态多级路由,但不确定采用什么方法。我需要的是:
/listing[/:directory1[/:directory2[/dir...]] 无限嵌套以模仿目录结构。
理想情况下,所有目录都应该以数组形式出现,或者至少像 "directory1/directory2/directory3...".
这样的单个变量
我找不到任何地方可以正确设置这些参数,即使 zf2 支持那种路由。我当前的路由配置如下所示:
...
'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',
),
),
),
),
),
...
两种可能的方法:
- 使用 regex route 并在控制器中选择 URL。
- 通过实施 RouteInterface 创建您自己的路线。
这是使用正则表达式路由的解决方案:
...
'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%',
),
),
...
内部控制器 $directory = $this->params()->fromRoute('dirPath'); returns 一个可以解析的 url 字符串。
我需要在 zf2 中构建动态多级路由,但不确定采用什么方法。我需要的是:
/listing[/:directory1[/:directory2[/dir...]] 无限嵌套以模仿目录结构。
理想情况下,所有目录都应该以数组形式出现,或者至少像 "directory1/directory2/directory3...".
这样的单个变量我找不到任何地方可以正确设置这些参数,即使 zf2 支持那种路由。我当前的路由配置如下所示:
...
'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',
),
),
),
),
),
...
两种可能的方法:
- 使用 regex route 并在控制器中选择 URL。
- 通过实施 RouteInterface 创建您自己的路线。
这是使用正则表达式路由的解决方案:
...
'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%',
),
),
...
内部控制器 $directory = $this->params()->fromRoute('dirPath'); returns 一个可以解析的 url 字符串。