可选参数 Zend Route Regex
如何使用 Zend 使路由正则表达式参数可选?
我尝试为搜索过滤器制作格式良好的 URL(“search?s=mp&t=w”而不是“search/index/s/mp/t/w”),例如。 :
受欢迎程度
- 最受欢迎 (s=mp)
- 浏览次数最多(s=mv,默认)
- 评分最高 (s=tr)
- 评论最多 (s=mc)
时段
- 所有时段(t=a,默认)
- 今天 (t=d)
- 本周(t=w)
- 这个月 (t=m)
因此,要从今天开始获得所有评分最高的项目,我将拥有: search?s=tr&t=d
使用正则表达式路由,我必须指定默认值,问题是 url视图助手生成带有默认值而不是当前值的链接。
这是我的路线:
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"
和链接:
<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>
例如,如果当前页面是“search?s=tr&t=d”并且我点击“本周”,则由于默认值,链接是:“search?s=mv&t=w”而不是“search?s=tr&t=w”。
我必须指定默认值,否则会出现错误。
有什么想法吗?
谢谢, 本杰明.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我没有使用正则表达式路由,但我看到了这个错误。基本上,
defaults.[param]
部分需要值。我的自定义路线,我将它们设置为空:I haven't used the regex routes, but I have seen this error. Basically, the
defaults.[param]
parts need values. I my custom route, I am setting them to be empty:您确定要这样做吗?有几次我尝试“对抗框架”,但发现框架更了解(笑)。我可以建议另一种方式吗? (谷歌可能更喜欢:让你的网址也更加“人性化”)。使用类似
将“小部件”替换为网站内容的 URL,例如“视频”、“博客文章”、“独轮车”等。
然后将上述每条路由发送到您的搜索控制器
are you sure you want to do it this way? a few times i've tried to "fight the framework" but found out the framework knew better (grin). can i suggest another way? (that Google may like better: makes your URLs more "people friendly" too). Use URLs like
where you replace "widgets" with whatever your site is about eg "videos", "blog posts", "unicycles" whatever.
then each of the above routes to your search controller