Search code examples
phpcakephpcakephp-3.4

Cakephp 3 query string build encoded &


I'm using CakePHP 3.5 Url Helper to generate some Urls to be called using jQuery Ajax. I have the following Ajax:

$(".container").on("change", "#select_box", function(){
                url = '<?= $this->Url->build(["controller" => "tools", "action" => "getTools", "?" => ["param1" => "PARAM1_PLACEHOLDER", "param2" => "PARAM2_PLACEHOLDER"]]); ?>';

            url = url.replace("PARAM1_PLACEHOLDER", $(this).val()).replace("PARAM2_PLACEHOLDER", 5);

            $("#tools").load(url);
        });

And for some reason, my url is coming up like this

/tools/get-tools/param1=1&amp;param2=5 (with &amp; instead of &)

Because of that, when I use getQuery('param2') to get the value of query string parameter param2, I get null.

Any help please? Isn't this the way that I'm supposed to build url with query string parameters in CakePHP 3.5?


Solution

  • You can pass the second parameter to not escape the &:

    $this->Url->build([
        "controller" => "tools",
        "action" => "getTools",
        "?" => [
            "param1" => "PARAM1_PLACEHOLDER",
            "param2" => "PARAM2_PLACEHOLDER"
        ]
    ], ['escape' => false]);