Cake replaces & with &

Hi coders,

I have a simple table like this:

CREATE TABLE omniais (
id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
isodatetime DATETIME NULL DEFAULT NULL,
nmdb FLOAT NULL DEFAULT NULL,
f10 FLOAT NULL DEFAULT NULL,
ap FLOAT NULL DEFAULT NULL,
pc FLOAT NULL DEFAULT NULL,
bz FLOAT NULL DEFAULT NULL,
dst FLOAT NULL DEFAULT NULL,
PRIMARY KEY (id) USING BTREE
)

I have a simple filter based on form

  <?= $this->Form->create(null,['type'=>'get'])?>

and I need to export filtered data to CSV file using the firends of cake extension CsvView. To do so, I added a function

public csv()

into my controller and I wanted to pass current search conditions using URL like this:

If I echo the $full_url, I am see correct url:

Nevertheless, if I click on DOWNLOAD CSV button, CakePHP returns this URL with ‘&amp’; instead of &:

http://girasol:81/omniais/csv?column_sel=nmdb&amp;bottom_limit=166.773&amp;upper_limit=166.774&amp;date_from=2003-03-26&amp;date_to=2024-03-26

As a consequence, I can not use getParam in csv() function of the same controller and I am getting back this:

All parameters instead of first one are determined in a wrong way as the have prefix ‘amp;’:

‘?’ => [
‘column_sel’ => ‘nmdb’,
‘amp;bottom_limit’ => ‘166.773’,
‘amp;upper_limit’ => ‘166.774’,
‘amp;date_from’ => ‘2003-03-26’,
‘amp;date_to’ => ‘2024-03-26’,
],

Can anybody please help me how to fix this issue? How to force not replacing ‘&’ with ‘&’?

I know that I could store the parameters into session but I am interested in the correct solution of this problem.

Thanks a lot in advance.

In your output, when you echo $full_url, it looks to you like it’s right. But is it? Take a look at the HTML source to confirm. Reason being that browsers will render &amp; as simply &; look up HTML entity decoding for more details.

This is just to help determine where the error actually lies. Your code looks right to me, and I don’t know why your URL would be encoded at any step of this.

Thank you Zuluru. I passed finally the parameteres other way, i.e. like this:

<?php
	$column_sel = $this -> request ->getQuery('column_sel');
    $bottom_limit = $this -> request ->getQuery('bottom_limit');
    $upper_limit = $this -> request ->getQuery('upper_limit');
	$date_from = $this -> request -> getQuery('date_from');
    $date_to = $this -> request -> getQuery('date_to');
	
	$full_url = $this->Url->build([
		'controller' => 'Omniais',
		'action'=>'csv',
		$column_sel,
		$bottom_limit,
		$upper_limit,
		$date_from,
		$date_to,
	]);

?>

Nevertheless, I would be curious, what causes this fault. I will check the page source ASAP.

It seeems that all URL in my page source have ‘&’ replaced with ‘&’:

I am afraid that the problem is elsewhere than in CakePHP. Has anyone ever met with similar problem? My computer uses Debian 12.4 / Apache 2 / PHP8.2 / MariaDB and localization is set to C (no localization was selected during installation).

Any hint what coul be wrong?

I am pretty sure cake double encodes your URL because you build the URL “twice”.

First in $this->Url->build() and then a secound time with $this->Html->link()

Try putting the whole URL array from your $this->Url->build() into the second param of $this->Html->link() instead of first building the URL yourself.

Thank you very much @KevinPfeifer . I still di not have time to test this solution but I will add result here later.

Thank you @KevinPfeifer once more. I modified my code today as follows:

And it works!

If you have logged in users where you know the E-Mail I’d personally put this export logic into a queue job and process it in the background. With that you only need to queue the export task with this button so the user immediatly sees, that its doing “something”.

And when the queued job is done processing, you can trigger e.g. a email with a download link to the CSV.

Thank you. This sounds to be more clever solution as sometimes the page get unresponsive due to long CSV creation. Unfortunately, I have to finish quicky this version and then I will have time to upgrade it. Once more thanks a lot.