I need to build dynamic multi level routing in zf2, but not sure what way to approach it. What i need is:
/listing[/:directory1[/:directory2[/dir...]] as unlimited nesting to mimic a directory structure.
Ideally all directories should come either as an array or at least a single variable like "directory1/directory2/directory3...".
i can not find anywhere ho to properly set up these parameters or even if zf2 supports that kind of routing. 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'); returns a url string that can be parsed.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.