Howto build a URL and pass two variables in Cake 4

In a Form I have an Ajax Request where I want to pass two variables to my controller:

xmlhttp.open(“GET”,“<?php echo $this->Url->build(['controller'=>'Cities','action'=>'findnearby','?'=>['location_id'=>'Variable 1','radius'=>'variable 2']]); ?>”);

In my Controller

debug($this->request->getQueryParams());

leads to this result:

APP/Controller/CitiesController.php (line 167 )

[
‘location_id’ => ‘Variable 1’,
‘amp;radius’ => ‘variable 2’, ]

How can I build a correct URL, so that the trailing “amp;” vanishes and I can use my variables?

for the first is solved it this way:

  var variables = "location_id=<?= $location->id ?>&radius="+ radius +"";
  xmlhttp.open("GET","<?php echo $this->Url->build(['controller'=>'Cities','action'=>'findnearby?']); ?>"+variables,true);

If there are better solutions (and I´m sure there are a lot of better solutions) please feel free to post them.

Heh, its past midnight so tired brain, I could be wrong, but I think it’s the escape parameter, set to false.
From here Url - 4.x

The 2nd parameter allows you to define options controlling HTML escaping, and whether or not the base path should be added:

$this->Url->build('/posts', [
    'escape' => false,
    'fullBase' => true,
]);

So its that bit about escape we are focusing on.

xmlhttp.open("GET","<?= $this->Url->build(['controller'=>'Cities',
   'action'=>'findnearby',
   '?'=>['location_id'=>'Variable 1','radius'=>'variable 2']],
  ['escape' => false]) ?>");

(I may have mucked that syntax up, but something like that.)

1 Like