2
votes

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.

3

3 Answers

3
votes

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.

1
votes

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.

0
votes

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.