How can Symfony2 routing be setup to allow more optional paths.
Currently the routing file looks like this, the controller contains 3 actions to handle the requests.
MediaFilmBundle_film:
pattern: /films
defaults: { _controller: MediaFilmBundle:Film:index }
MediaFilmBundle_Film_Genre:
pattern: /films/genre/{genre_slug}
defaults: { _controller: MediaFilmBundle:Film:genre }
MediaFilmBundle_Film_Source:
pattern: /films/source/{source_slug}
defaults: { _controller: MediaFilmBundle:Film:source }
This work but how can it be made better or is the following beyond Symfony2, is it possible to do something like the following so there is only one action that handles it all.
MediaFilmBundle_film:
pattern: /films/genre/{genre_slug}/source/{source_slug}
defaults: { _controller: MediaFilmBundle:Film:index }
The above route should match all of the following:
/films
/films/genre/horror
/films/source/america
/films/genre/action/source/england
Ideally it should not match the following or the controller could redirect it to the standard format genre/../source/..
/films/source/england/genre/action
The route could be matched using the existing structure like so:
MediaFilmBundle_Film_Genre_Source:
pattern: /films/genre/{genre_slug}/source/{source_slug}
defaults: { _controller: MediaFilmBundle:Film:genreSource }
The problem with this approach is as more slugs are added more routes and more actions are needed.
- 1 slug = 2 routes and actions
- 2 slugs = 4 routes and actions
- 3 slugs = 8 routes and actions
- n slugs = ...