Php – How to specify a dynamic default for a key in a router, Zend Framework

PHProutingzend-framework

This question is linked to this one

How can I set the default of the category part to be the category value in the request url?

$Router=$this->_front->getRouter();
$CategoryRoute = new Zend_Controller_Router_Route('category/:category/:controller/:action/*',
           array(
                 'controller' => 'index',
                 'action'     => 'index',
                 'category'   => 'aaa'
           ));
$Router->addRoute('category', $CategoryRoute);

In other words, I need the value [aaa] to be the value of category in the time I am building this route. There will always be a value for [category] as otherwise it will use the default route.

Example of what I mean:
If I surf to the site with url http://baseurl/category/mycat/index
I will be routed to controller=index, action=index, category=mycat.
But, in all my view files, where I use the Zend_View::url() helper, the links will point to:
http://baseurl/category/aaa/somthing/somthing (Using the exact route from above)
While I actually need them to point to:
http://baseurl/category/mycat/somthing/somthing

This happens because the default value for category is written as a constant in the route, and not taken, somehow, from the current URL.
I currently solve this by extracting by myself the category from the URL and making it the default.

Best Answer

Is there a question in your post? :)

IF I got you right - yousetup your default category and it will be used if you call

$this->url(array(),'category');

But when you use

$this->url(array('category'=>'my-category'),'category');

it will return category/my-category/index/idnex instead of category/aaa/index/index

Related Topic