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

时间:2016-08-31 18:38:42

标签: symfony symfony-2.8

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

1 个答案:

答案 0 :(得分:2)

  

默认情况下,Symfony Routing组件需要参数   匹配以下正则表达式路径:[^ /] +。这意味着所有人物   允许除了/.

     

您必须通过指定明确允许/成为参数的一部分   一个更宽松的正则表达式路径。

您的路线定义必须使用正则表达式:

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

我没有测试过代码,但它基本上说你要添加一个额外的参数,可能是也可能不是/

代码片段的灵感来源于官方文档:

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;

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

在此处查找更多内容:http://symfony.com/doc/current/routing/slash_in_parameter.html