Hello,
How to make route regex parameters optionals with Zend ?
I try to make well formatted URLs ("search?s=mp&t=w" instead of "search/index/s/mp/t/w") for search filters, ex. :
Popularity
- Most popular (s=mp)
- Most viewed (s=mv, default)
- Top rated (s=tr)
- Most commented (s=mc)
Period
- All period (t=a, default)
- Today (t=d)
- This week (t=w)
- This month (t=m)
So, to get all top rated items from today i will have : search?s=tr&t=d
With regex routes i must specify defaults values and the problem is that the url view helper generates links with the default values and not with the current values.
Here is my route :
resources.router.routes.search.type = "Zend_Controller_Router_Route_Regex"
resources.router.routes.search.route = "search\?s\=(.+)\&t\=(.+)"
resources.router.routes.search.map.1 = s
resources.router.routes.search.map.2 = t
resources.router.routes.search.defaults.module = front
resources.router.routes.search.defaults.controller = search
resources.router.routes.search.defaults.action = index
resources.router.routes.search.defaults.s = mv
resources.router.routes.search.defaults.t = a
resources.router.routes.search.reverse = "search?s=%s&t=%s"
and links :
<div class="filters note">
<div class="filters-content">
<h3>Popularity</h3>
<ul class="filters-list">
<li>
<a href="<?=$this->url(array('s' => 'mp'), 'search')?>">
Most popular
</a>
</li>
<li>
<a href="<?=$this->url(array('s' => 'mv'), 'search')?>">
Most viewed
</a>
</li>
<li>
<a href="<?=$this->url(array('s' => 'tr'), 'search')?>">
Top rated
</a>
</li>
<li>
<a href="<?=$this->url(array('s' => 'mc'), 'search')?>">
Most commented
</a>
</li>
</ul>
</div>
</div>
<div class="filters period">
<div class="filters-content">
<h3>Period</h3>
<ul class="filters-list">
<li>
<a href="<?=$this->url(array('t' => 'a'), 'search')?>">
All period
</a>
</li>
<li>
<a href="<?=$this->url(array('t' => 'd'), 'search')?>">
Today
</a>
</li>
<li>
<a href="<?=$this->url(array('t' => 'w'), 'search')?>">
This week
</a>
</li>
<li>
<a href="<?=$this->url(array('t' => 'm'), 'search')?>">
This month
</a>
</li>
</ul>
</div>
</div>
For example, if current page is "search?s=tr&t=d" and i clic on "This week", the link is : "search?s=mv&t=w" instead of "search?s=tr&t=w" because of the default values.
I must specify default values or i get an error.
Any idea ?
Thanks, Benjamin.