1

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?

Teshan N.
  • 2,307
  • 3
  • 30
  • 59

1 Answers1

3

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}
stephan.mada
  • 1,110
  • 9
  • 11
  • 1
    That would only work if id is numeric and still seems a bit fragile. Simply swapping the order of the routes is probably a better choice. – Cerad May 07 '19 at 11:48