Symfony2: URLs with trailing slash and an optional parameter

routes, symfony

Solution

In a separate but related case, you can match the following three patterns:

/foo
/foo/
/foo/page/1

by eliminating the trailing backslash in the route and relaxing the regular expression to match 0 or more characters (*), instead of 1 or more (+):

foo_route:
    pattern:  /foo/{page}
    defaults: { _controller: FooBundle:Foo:list, page: 1 }
    requirements:
      page:  \d*

However that will not match /foo/page/1/

Problem

I want all URLs in my application to have a trailing slash. I have the following route in my route.yml: ``` foo_route: pattern: /foo/{page}/ defaults: { _controller: FooBundle:Foo:list, page: 1 } requirements: page: \d+ ``` Requests to '/foo/1/' work fine, however requests to '/foo/' are not matched because of the trailing slash in the URL pattern. How can I define routes with trailing slashes and an optional parameter? I am aware I can define 2 different routes for the two cases, but I want to avoid that.

Original source