views:

518

answers:

3

Say that I have to following 2 routes in this order:

Zip:
  url: home/:zip
  param: { module: home, action: results }

State:
  url: home/:state
  param: { module: home, action: results }

and I use a route such as:

'@State?state=CA'

Why does it resolve to the Zip route? I thought when you specified the route name explicitly: '@State' that it did not parse the entire routing file and just used the specific route you asked for.

I would like to be able to use the same action to display data based on the variable name (zip or state) I don't want to have to create the same action (results) twice just to pass in a different parameter.

+1  A: 

You need to add requirements so that it will recognize the different formats

Zip:
  url: home/:zip
  param: { module: home, action: results }
  requirements: { zip: \d{5} } # assuming only 5 digit zips

State:
  url: home/:state
  param: { module: home, action: results }
  requirements: { state: [a-zA-Z]{2} }

That should fix it, although I agree with you that when you use a route name in a helper it should use the named route.

Peter Bailey
Ahh, ok...so it doesn't actually resolve to the route name, it still does some parsing--what's the point of a unique route name then?
Failpunk
I wish I could drink your icon right about now.
Failpunk
I'd need to see more code in how you're leveraging the route name. I assume this is in a view helper (like link_to or url_for) but I'm not sure. Also, what version of symfony?
Peter Bailey
I am using Symfony 1.2.1, the route is being called from a url_for() statement.
Failpunk
A: 

It does use the named route. The issue is that your URLs are the same (the paramater name is irrelevant) so when you load the URL in your browser, the first route to match will always be Zip as there is no way for Symfony to know which one you want.

Adding requirements: is the way to go.

laurentb
I guess there is still a bit of parsing in there? I would think that if you specified a specific route name, it would disregard all other routes and use only the named route--thus having the same url would not be a problem.
Failpunk
+1  A: 

the problem here is that both routing rules resolves to somewhat same url ( /home/) and when generating links it uses correct named route to generate url.

when you click on that generated url( for example /home/CA) withouth requirements addition it will match the first rule in routing.yml file that matches and that is your "Zip" route.

just to explain what is happening.

deresh