Zend Router precedence

2019-08-01 08:58发布

问题:

I have got two routes

; 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+

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

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

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.

回答1:

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.

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.

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.

Cheers



回答2:

Try putting searchroute definition before category route definition in your config file.

The idea is that more specific routes (searchroute in your case) should precede more general routes (category route).