I'm using CakePHP 3.4+ for my application.
There is an XML file in local path and needs to be downloaded when on clicking the link. I want to return download from controller's action after checking few requirements as per my application
public function downloadXml()
{
if ($this->_checkMembership()) {
try {
// file path /webroot/agency/data.xml
$xmlLink = WWW_ROOT . 'agency/data.xml';
$this->response->withFile($xmlLink, [
'download' => true,
'name' => 'data.xml',
]);
return $this->response;
} catch (NotFoundException $e) {
$this->Flash->error('Requested file not found. Try again');
return $this->redirect(['action' => 'index']);
}
}
}
And in template
<?= $this->Html->link(
__('Download the site'),
[
'action' => 'downloadXml'
],
) ?>
But this only shows a blank page on clicking the link