处理URL参数上的斜杠 [英] Handling slashes on URL parameters
问题描述
我遇到了一些问题,试图猜测如何使用Symfony + Twig上的参数处理URL.
I'm running into some issues trying to guess how to handle URLs with parameters on Symfony+Twig.
我有这条路线:
<route id="artist.front" path="/artist/{kw}/">
<default key="_controller">App\Web\Controllers\Front::homePage</default>
</route>
以下是Twig模板上的代码:
And this code on the Twig template:
{% autoescape false %}
<a href="{{ path('artist.front',{'kw':a.urlkeyword|url_encode} )}}">{{ a.name }}</a>
{% endautoescape %}
urlkeyword
参数按原样(即先前未编码的URL)在实体构造函数及其getter(getURLKeyword)上传递.
urlkeyword
parameter is passed as is (i.e. not URL encoded previously) both on the entity constructor and on its getter (getURLKeyword).
但是,在渲染模板时,我得到了以下HTML代码:
However, when rendering the template I get this HTML code:
<a href="/index.php/artist/AC%252FDC/">AC/DC</a>
(如果我从模板中删除url_encode,Twig会引发调用path()的错误.)
(If I remove the url_encode from the template Twig throws an error calling path()).
目标控制器具有以下代码:
The target controller has this code:
public function artistPage($kw)
{
$decoded = urldecode($kw);
$this->log->info("kw: {$kw}");
$this->log->info("decoded: {$decoded}");
}
并回声:
kw: AC%2FDC
decoded: AC/DC
所以我认为可以从控制器正确读取kw,但我知道应该将URL编码为AC%2FDC,而不是首先放在最终的HTML代码上:URLEncoded字符串被编码两次.
so I think the kw is read correctly from the controller, but I understand the URL should be encoded as AC%2FDC instead on the final HTML code on the first place: The URLEncoded string is encoded twice.
AC%2FDC是正确的编码,还是有必要在Symfony中使用双重编码(AC%252FDC)?而且,如果可以使用第一种(正确的)URL编码,那么我在做什么错了?
Is AC%2FDC the correct encoding, or is it necessary to use the double encoding (AC%252FDC) in Symfony? And, if it is possible to use the first (correct) URL encoding, what I am doing wrong?
推荐答案
尝试:
<route id="artist.front" path="/artist/{kw}/">
<default key="_controller">App\Web\Controllers\Front::homePage</default>
<requirement key="kw">.+</requirement>
</route>
并删除网址编码/解码.
And remove the url encoding/decoding.
这篇关于处理URL参数上的斜杠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!