Symfony 2.1+ 编码散列/磅“#"在路线中 [英] Symfony 2.1+ encodes hash/pound "#" in routes
问题描述
我正在从 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没有被解码.在提交中添加了对标签的转义 UrlGenerator="">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: Acme\MyBundle\Routing\HashtagDecodedUrlGenerator
这篇关于Symfony 2.1+ 编码散列/磅“#"在路线中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!