chrislebaron
chrislebaron

Reputation: 279

Is it possible to make Symfony ignore a double slash in the URL

I'm writing a new endpoint for 3rd party desktop application, and there are several different functions in the application that post to the same endpoint on my Symfony 2.8 server.

Sometimes the desktop application goes to the correct path - example.com/path/to/endpoint. However sometimes it tries to go to add an extra slash in between the domain name and the path - example.com//path/to/endpoint.

I tried to just add an extra route with the double slash in it like this:

/**
 * @Route("/path/to/route", name="example_route")
 * @Route("//path/to/route", name="example_route_double_slash")
 * @Method({"POST"})
 */

Symfony just ignores the double slash when it compiles the routes though, and I end up with 2 of "/path/to/route" if I check my routes with app/console debug:router

Upvotes: 1

Views: 1722

Answers (1)

zizoujab
zizoujab

Reputation: 7800

By default, the Symfony Routing component requires that the parameters match the following regex path: [^/]+. This means that all characters are allowed except /.

You must explicitly allow / to be part of your parameter by specifying a more permissive regex path.

Your route definition have to use regex :

 /**
 * @Route("/{slash}/path/to/route", name="example_route_double_slash", requirements={"slash"="\/?"})
 */

I haven't tested the code but it basically says that you are adding an extra parameter that may or may not be a /.

The code snippet is inspired from this one at official docs :

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;

class DemoController
{
    /**
     * @Route("/hello/{username}", name="_hello", requirements={"username"=".+"})
     */
    public function helloAction($username)
    {
        // ...
    }
}

find more here : http://symfony.com/doc/current/routing/slash_in_parameter.html

Upvotes: 3

Related Questions