5

I encoded an url parameter with the twig filter url_encode.

// app.request.query.get("date") output 01/04/2016

href="{{ path('page', {date: app.request.query.get("date")|url_encode}) }}">

which output in the url

date=01%252F04%252F2016

So in the requested page with url parameters

 {{ app.request.query.get("date") }}

display 01%2F04%2F2016 but i'd like to have 01/04/2016

I tried using raw filter and also did a twig extension :

<?php
namespace SE\AppBundle\Twig;

class htmlEntityDecodeExtension extends \Twig_Extension
{
    public function getFilters()
    {
        return array(
            new \Twig_SimpleFilter('html_entity_decode', array($this, 'htmlEntityDecode'))
        );
    }

    public function htmlEntityDecode($html)
    {
        $html = html_entity_decode($html);
        return $html;
    }

    public function getName()
    {
        return 'html_entity_decode_extension';
    }
}

But even with this it keeps displaying 01%2F04%2F2016

I get the same result in my controller method with :

echo html_entity_decode($request->query->get('date'));

What is the proper way to do this?

UPDATE :

the date is from an input of type 'text'. No this is a simple string with numbers and / .

Brieuc
  • 3,994
  • 9
  • 37
  • 70

1 Answers1

5

There's no need to url-encode the parameter of the query string in the first place, as it's already done by the function that generate the path.

01%252F04%252F2016 is doubly urlencoded. PHP, when gets the request, already decode that value to 01%2F04%2F2016, but since you encoded it twice, it's still urlencoded. You need to use urldecode function to decode it. Or even better: don't urlencode it twice.

It's fine:

{{ path('page', {date: app.request.query.get("date")}) }}

UPDATE

Found this in the source code:

// "/" and "?" can be left decoded for better user experience, see
// http://tools.ietf.org/html/rfc3986#section-3.4
$url .= '?'.(false === strpos($query, '%2F') ? $query : strtr($query, array('%2F' => '/')));

So, the / is left deliberately url-decoded.

Federkun
  • 36,084
  • 8
  • 78
  • 90
  • That is the weird thing, this output date=01/04/2016 – Brieuc Apr 09 '16 at 10:45
  • where did you see that output? check from the html source of the page – Federkun Apr 09 '16 at 10:48
  • But urldecode() seems to do the job instead of html_entity_decode. – Brieuc Apr 09 '16 at 10:49
  • This output is from url not from html. This is why i encoded it at the first place. – Brieuc Apr 09 '16 at 10:51
  • 1
    So, check the html, and check if the generated path is url-encoded correctly: something like `/page/?data=01%2F04%2F2016`. Then your job is done. If your browser shows you an already url-decoded uri, it's just a deception of the browser. – Federkun Apr 09 '16 at 10:55
  • The html is still ?from_date=01/04/2016 (By the way the date parameter is a string). – Brieuc Apr 09 '16 at 10:59
  • what's your symfony version? there's something that you didn't show us? – Federkun Apr 09 '16 at 11:03
  • 2.7, i will switch to 3 really soon. I think i showed everything. I'll update the post with the fact i'm using an input of type text, so not a standard date format. – Brieuc Apr 09 '16 at 11:04
  • 1
    just tried by myself, with surprise I get your same result - give me a minute to check why – Federkun Apr 09 '16 at 11:12