Symfony: Prefix route automatically adds trailing slash if a pattern is empty

Created on 16 Aug 2011  路  1Comment  路  Source: symfony/symfony

Prefix route automatically adds trailing slash if a pattern is empty.

Example:

In the Routing.xml:
import resource="@AcmeTestBundle/Controller/TestController.php" prefix="/test" type="annotation"

In the FrameworkExtraBundle annotations:

  • @Route("", name="test")

Whenever you go to /test SF2 will automatically redirect it to /test/

If you add a dummy variable however, and give it a default value it is not being redirected, example:

  • @Route("{dummy}", defaults={"dummy":1} name="test")
Routing

>All comments

The behavior is indeed correct. Let me explain why.

First, a / is always prepended to a route if it is not already the case. So, an empty route is equivalent to / and when you define a {dummy} route pattern, this is equivalent to /{dummy}.

When you use /test as a prefix, you are mounting all routes under a /test "directory", so all defined routes are "inside" this "directory".

For the first case, and empty string or / means the root of the directory, and if you omit the /, Symfony will automatically redirects you to this page (the same behavior as Apache), but when you use /{dummy}, you are not referencing the root anymore (even if there is a default value), so the redirection does not occur.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jpresley23 picture jpresley23  路  3Comments

HappyJiJi picture HappyJiJi  路  3Comments

davidbarratt picture davidbarratt  路  3Comments

kherrera-ebsco picture kherrera-ebsco  路  3Comments

sunviwo picture sunviwo  路  3Comments