Search code examples
symfonyurl-routing

Symfony route not working with default and trailing /


I'm beginning to work with Symfony 3. Testing routes, I created one with defaults as following:

index:
    path:     /test/{name}
    defaults: { _controller: MainBundle:Advert:index, name: maxime }

The route works with:

  • /test/randomname
  • /test

But not with

  • /test/

Any idea why ? thanks


Solution

  • This is documented here https://symfony.com/doc/master/routing/optional_placeholders.html

    Routes with optional parameters at the end will not match on requests with a trailing slash (i.e. /blog/ will not match, /blog will match).

    If you do need to match even /test/ you can add the following route entry

    index_trailing_slash:
        path:     /test/
        defaults: { _controller: MainBundle:Advert:index, name: maxime }