是否可以让 Symfony 忽略 URL 中的双斜杠

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

我正在为第 3 方桌面应用程序编写一个新端点,应用程序中有几个不同的函数 post 到我的 Symfony 2.8 服务器上的相同端点。

有时桌面应用程序会转到正确的路径 - example.com/path/to/endpoint。但是有时它会尝试在域名和路径之间添加一个额外的斜杠 - example.com//path/to/endpoint.

我试着像这样添加一个带有双斜杠的额外路由:

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

Symfony 在编译路由时只是忽略了双斜杠,如果我用 app/console debug:router

检查我的路由,我最终会得到 2 个“/path/to/route”

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.

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

 /**
 * @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