简体   繁体   中英

Zend framework 1: Conflict with my routes (routes.ini)

I've defined some routes in a routes.ini file for a simple Q&A app:

; e.g. /5/slug-of-the-title
routes.show_question.route = ":id/:slug"
routes.show_question.defaults.controller = "questions"
routes.show_question.defaults.action = "get"
routes.show_question.defaults.slug = null

; e.g. /ask
routes.ask.route = "ask"
routes.ask.defaults.controller = "questions"
routes.ask.defaults.action = "new"

; .. below this, similar route patterns e.g. "register", "login", etc

Now this works, I can access the following paths of my app:

/5/slug-of-the-title
/5
/ask
/register
/login

However, it only works if ":id/:slug" is declared at the top. Otherwise I get an error saying "id is not specified". I thought the bottom would make sense as the other routes would fail to match (/5 doesn't match "ask"), but seems the top worked out. Why is this?

Also, routes that previously worked such as /account (which defaulted to account controller and index action) doesn't work anymore. Do I have to declare all the previously-working-by-default-routes? I was hoping to just declare exceptional routes and leave the others to be handled by default (:controller/:action).

By the way, I did try to declare a "catch all" route:

; catch all
routes.ask.route = ":controller/:action"
routes.ask.defaults.controller = "index"
routes.ask.defaults.action = "index"

I thought the logical place to declare it would be the bottom again. My understanding was that Zend would try to match routes in order on the script, and when none matched, this would be the default route. But, it doesn't matter where I put it - top, bottom, after ":id/:slug" at least one page will give me the "id is not specified" error. Can anyone explain where I'm going wrong? Thanks

Routes are matched in reverse order so place your most generic routes first. But the issue here is that '/account' will match the route ':id/:slug', because you don't request that :id must be an numeric value. To do that, just add this line to your route definition :

routes.show_question.reqs.id = "\d+"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM