Symfony 2.1+ 对哈希/磅“#"进行编码在路线中路线、Symfony、quot

由网友( つ゜ 她那么想她。  )分享简介:我正在从 Symfony 2.0 升级到 2.3.因为我们有一个单页应用程序,所以我们定义了带有哈希的路由.I'm in the process of upgrading from Symfony 2.0 to 2.3. We have routes with hashes defined since we have...

我正在从 Symfony 2.0 升级到 2.3.因为我们有一个单页应用程序,所以我们定义了带有哈希的路由.

I'm in the process of upgrading from Symfony 2.0 to 2.3. We have routes with hashes defined since we have a single page app.

通过注解配置的路由:

/**
 * @Route("/app#orders/{id}", name="app_order")
 */

我们使用 Twig 生成电子邮件并在 Twig 模板中使用这些路由:

We use Twig to generate emails and use these routes within the Twig templates:

<a href="{{ url('app_order', { 'id': '123' }) }}">View order</a>

在升级之前,这工作正常.升级后,# 被编码为 %23,但斜线保持不变.这当然会在电子邮件中生成无效的 URL.

Before upgrading, this worked fine. After the upgrade, the # gets encoded to %23 but the slashes remain intact. This of course generates an invalid URL in the email.

为什么只有哈希编码而不是斜杠?它应该是全部或全部.除了进行字符串替换之外,我还有哪些选择?

Why are only the hashes encoding and not the slashes? It should be all or nothing. What options do I have here other than doing a string replace?

我已经尝试过但无济于事的事情:

Things I've already tried doing which don't help:

将 autoescape 设置为 false {% autoescape false %}使用原始 {{ url(...)|raw }}结合使用 raw 和 autoescape=false

推荐答案

如果你看看 UrlGenerator 代码,可以看到,在rawurlencode之后没有解码hashtag.在提交 6039569.

If you take a look at the UrlGenerator code, you can see, the hashtag is not decoded after rawurlencode. The escaping of the hashtag was added in commit 6039569.

作为一种解决方法,您可以扩展 UrlGenerator 类并将 $decodedChars 数组替换为包含的主题标签.然后告诉 Symfony 使用你的生成器类:

As a workaround you could extend the UrlGenerator class and replace the $decodedChars array with hashtag included. Then tell Symfony to use your generator class:

parameters:
    router.options.generator_base_class: AcmeMyBundleRoutingHashtagDecodedUrlGenerator
阅读全文

相关推荐

最新文章