0
votes

I have a problem with Symfony routing. Even though I use different parameters to in the paths of two different routes Symfony identifies as a one pattern and directs to the path which is defined first in the routing file. For ex:

app_restaurants_inner:
    path:     /london-restaurants/{id}/{restaurant_name}.html
    defaults: { _controller: AppBundle:Restaurants:inner}

app_restaurants_by_cuisine:
    path:     /london-restaurants/cuisine/{cuisine}.html
    defaults: { _controller: AppBundle:Restaurants:index}

First route loads a specific restaurant and the parameters are id and restaurant name. Restaurant names only contains a-z, 0-9 and hyphens. In the second one there is only one parameter which is cuisine. But when I try to load a cuisine (2nd route) it directs me to the restaurant path which has a similar path as cuisine.

On the other hand the following route is also identified similar to the restaurant's path.

app_restaurants_by_cuisine_letter:
    path:     /london-restaurants/cuisine/{cuisine}-{letter}.html
    defaults: { _controller: AppBundle:Restaurants:index}

The word 'cuisine' is identified as '{id}' and '{cuisine}-{letter}' is identified as '{restaurant_name}'.

How can I fix this?

1
You have to put route 'app_restaurants_by_cuisine' just above 'app_restaurants_inner'.Mitesh Vasava
Mitesh Vasava is correct. order matters. move the more specific route above the other and you should be fine...Jakumi

1 Answers

3
votes

You should add some requirements in your route definitions Adding {wildcard} Requirements

app_restaurants_inner:
    path:     /london-restaurants/{id}/{restaurant_name}.html
    defaults: { _controller: AppBundle:Restaurants:inner}
    requirements:
        id: '\d+'

app_restaurants_by_cuisine:
    path:     /london-restaurants/cuisine/{cuisine}.html
    defaults: { _controller: AppBundle:Restaurants:index}