Zend 路由器优先级 [英] Zend Router precedence

查看:26
本文介绍了Zend 路由器优先级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两条路线

; category route
resources.router.routes.category.type = "Zend_Controller_Router_Route"
resources.router.routes.category.route = "shopping/:idTwo/:id/*"
resources.router.routes.category.defaults.module = "default"
resources.router.routes.category.defaults.controller = "shopping"
resources.router.routes.category.defaults.action = "category"
resources.router.routes.category.reqs.id = \w+
resources.router.routes.category.reqs.id = \d+

; searchroute
resources.router.routes.search.type = "Zend_Controller_Router_Route"
resources.router.routes.search.route = "shopping/search/:id/*"
resources.router.routes.search.defaults.module = "default"
resources.router.routes.search.defaults.controller = "shopping"
resources.router.routes.search.defaults.action = "search"
resources.router.routes.search.reqs.id = \w+

类别路由必须与诸如 http://mrc.localhost/shopping/Childrens- 之类的 url 匹配服装/98,效果很好

Category route must match with urls like http://mrc.localhost/shopping/Childrens-Clothing/98 and it is working fine

搜索路径必须与 http://mrc.localhost/shopping/等网址匹配搜索/DVD+盒子+设置http://mrc.localhost/shopping/search/123.

Search route must match with urls like http://mrc.localhost/shopping/search/dvd+box+set and http://mrc.localhost/shopping/search/123.

搜索路线对于没有关键字(:id)作为整数的网址工作正常,例如http://mrc.localhost/shopping/search/dvd+box+set 但如果关键字只是整数,那么类别路由器优先于 urls http://mrc.localhost/shopping/search/123 并且因此调用类别操作而不是搜索操作,因为您看到 :idTwo 可以是任何字符串并且它匹配搜索",因此使用类别路由,但我希望无论关键字是什么都使用搜索路由.

Search route is working fine for url that dont have keyword(:id) as an integer like http://mrc.localhost/shopping/search/dvd+box+set but if keyword is just integer then category router took precende like for urls http://mrc.localhost/shopping/search/123 and therefore category action is called instead of search action because you see :idTwo can be any string and it matches "search" therefore category route is used but I want search route to be used no matter what is the keyword.

推荐答案

路由实际上是按照 REVERSE 顺序匹配的,所以将更具体的路由放在底部,将更通用的路由放在顶部.您的订单没问题.

Routes are actually matched in REVERSE order, so put your more specific routes on the bottom, and more generic ones on top. Your order is fine.

当我测试时,我发现 ID 为 (123) 的搜索 URL 工作正常,但另一个 ('dvd+box+set') 不起作用.您需要 :id 为 \w+ -- 'dvd+box+set' 中的 + 是导致它失败的原因.

When I tested, I found that the search URL with the ID (123) works fine, but the other one ('dvd+box+set') does not work. You are requiring :id to be \w+ -- the + in 'dvd+box+set' is what is causing it to fail.

如果您希望 search/* 转到搜索操作,请放弃该要求,并在必要时在您的操作控制器代码中对其执行更多操作,否则非 \w+ id 将导致它转到类别路径.

If you want search/* to go to the search action, ditch the requirement, and do something more with it in your action controller code if you must, otherwise non \w+ ids will cause it to go to the category route.

干杯

这篇关于Zend 路由器优先级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆