Symfony routing problem, route being ignored
I have the following in my app/frontend/config/routing.yml
:
homepage: url: / param: { module: main, action: index } default_index: url: /:module param: { action: index } default: url: /:module/:action/* sf_guard_register: url: /register param: { modu开发者_如何学Pythonle: user, action: register } sf_guard_signin: url: /login param: { module: sfGuardAuth, action: signin } sf_guard_signout: url: /logout param: { module: sfGuardAuth, action: signout } sf_guard_password: url: /request_password param: { module: sfGuardAuth, action: password }
When I try the /register
route, I get this:
Action "register/index" does not exist.
I should specify, I don't have a "register" module, and my user module is working well if called manually. Plus the other routes are working fine.
Any ideas are welcomed, thank you!
In line with jeremy's answer:
Because your default_index
route is placed above, going to /register
causes this route to match. Since it is located before the sf_guard_register
route, only the first match "counts".
Your routing.yml should look like this:
sf_guard_register:
url: /register
param: { module: user, action: register }
sf_guard_signin:
url: /login
param: { module: sfGuardAuth, action: signin }
sf_guard_signout:
url: /logout
param: { module: sfGuardAuth, action: signout }
sf_guard_password:
url: /request_password
param: { module: sfGuardAuth, action: password }
homepage:
url: /
param: { module: main, action: index }
default_index:
url: /:module
param: { action: index }
default:
url: /:module/:action/*
Also, there are a number of best-practises that you might want to follow:
- Disabling the default routes and relying solely on named routing such as
@homepage
is faster than having symfony figure out which route to match when usingurl_for
orlink_to
helpers. Therefore, disable the default routing and create the routes yourself. Then, use the name of the route when generating URLs. - The routes used most often should be placed at the top, for example if you have a register link on all pages, you should place
sf_guard_register
at the top.
Just my 5 cents - hope you find it useful ;-)
Routes are processed in the order they are listed in your routing.yml
file. Thus, the url /register matches the default_index route before it gets to sf_guard_register. Both default_index and default should go at the bottom of your routing.yml
file.
You should even delete the 2 default routes :
default_index:
url: /:module
param: { action: index }
default:
url: /:module/:action/*
As it is bad practice to use them, and should always create a specific route.
The best way to add routes, is to PREPEND them to you file, adding each new route at the very top.
Also, a good way to debug your routing in frontend is to run :
symfony app:routes frontend
so you will see wich route maches what (and its requirements if any)
精彩评论