【问题标题】:Is it possible to make Symfony ignore a double slash in the URL是否可以让 Symfony 忽略 URL 中的双斜杠
【发布时间】:2016-08-31 18:38:41
【问题描述】:

我正在为第 3 方桌面应用程序编写一个新端点,应用程序中有几个不同的函数可以发布到我的 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”

【问题讨论】:

  • 不完全是您问题的答案,但要解决根本问题,您可能需要查看bugs.launchpad.net/ubuntu/+source/php5/+bug/1511414
  • 我不认为这是我想要的方向......看起来这个链接中讨论的问题已在 php-5.5.17 上修复,我在 php Ubuntu 上的 5.6.19 用于我的生产服务器,而我的 Mac 上的 php 5.6.22 用于开发。两者都不起作用。

标签: symfony symfony-2.8


【解决方案1】:

默认情况下,Symfony 路由组件要求参数 匹配以下正则表达式路径:[^/]+。这意味着所有字符 允许除 /。

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

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

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

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

代码 sn-p 的灵感来自官方文档中的代码:

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

【讨论】:

    猜你喜欢
    • 2018-11-17
    • 2019-06-25
    • 1970-01-01
    • 2012-06-19
    • 1970-01-01
    • 1970-01-01
    • 2018-05-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多