Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
951 views
in Technique[技术] by (71.8m points)

php - Handling slashes on URL parameters

I'm running into some issues trying to guess how to handle URLs with parameters on Symfony+Twig.

I have this route:

<route id="artist.front" path="/artist/{kw}/">
<default key="_controller">AppWebControllersFront::homePage</default>
</route>

And this code on the Twig template:

{% autoescape false %}
<a href="{{ path('artist.front',{'kw':a.urlkeyword|url_encode} )}}">{{ a.name }}</a>
{% endautoescape %}

urlkeyword parameter is passed as is (i.e. not URL encoded previously) both on the entity constructor and on its getter (getURLKeyword).

However, when rendering the template I get this HTML code:

<a href="/index.php/artist/AC%252FDC/">AC/DC</a>

(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}");
}

and echoes:

kw:      AC%2FDC 
decoded: AC/DC 

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.

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

try:

<route id="artist.front" path="/artist/{kw}/">
    <default key="_controller">AppWebControllersFront::homePage</default>
    <requirement key="kw">.+</requirement>
</route>

And remove the url encoding/decoding.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...