I'm trying to write a route for an level N category depth. So an usual category URL would look like this:
http://website/my-category/my-subcategory/my-subcategory-level3/my-subcategory-level4
It has an unknown depth and my route has to match all possible levels. I made a route for this, but I can't get all the params from my controller.
$routeCategory = new Zend_Controller_Router_Route_Regex(
'(([a-z0-9-]+)/?){1,}',
array(
'module' => 'default',
'controller' => 'index',
'action' => 'index'
),
array( 1 => 'path'),
'%s'
);
$router->addRoute('category', $routeCategory);
I can't seem to find a way to send the route matched params to the controller. If you have a better solution, I'm open to suggestions!
For anyone stumbling across this question using Zend Framework 2 or Zend Framework 3, there is a regex route type which can (and probably should) be used for the OP's route in which there is an unknown number of parameters dependent on the number of child categories. To use, add the following line to the top of your router config:
Then, you can use a route such as the following to match an unknown number of categories:
The above route will match the following routes:
... and so on. The sequence of categories is passed to the controller in the
sequence
parameter. You can process this parameter as desired in your controller.I've done it without routes... I've routed only the first parameter and then route the others getting all the params inside the controller
Route:
as example: I use this for the catalog, then into the itemController into the displayAction I check for $this->getRequest()->getParams(), the point is that you can (but I think that you know it) read all the params passed in the way key/value, as example: "site.com/catalog/item/15/kind/hat/color/red/size/M" will produce an array as:
$params['controller'=>'catalog','action'=>'display','id'=>'15','kind'=>'hat','color'=>'red','size'=>'M'];
I found a solution that I think fits my needs. I'll post it here for people who will end up in the same thing I got into.
Problem:
category/subcategory/subsubcategory/...
category/subcategory/../page.html
admin
for example)Solution:
Zend_Controller_Router_Route_Regex
as a starting point so I can benefit from theassemble()
method)Actual code:
Usage:
Route:
URL Helper:
Sample output in controller with getAllParams()
cmsObject
is set only when the URL contains something likecategory/subcategory/subsubcategory/my-page.html