Hi,
I’m creating an API in wich I need to return some News as an XML file.
Im using CakePhp version 3.4.6.
Here’s what I’ve done so far:
In config/routes.php
:
I’ve added $routes->extensions(['xml']);
In Controller/NewsController.php
:
public function api()
{
$this->viewBuilder()->className(‘Xml’);
$news = $this->News
->find()
->order([‘date’ => ‘DESC’]);
$this->set(compact(‘news’));
$this->set(’_serialize’, [‘news’]);
}
In Template/News/xml/api.ctp
:
In Template/Layout/xml/xml.ctp
:
So far, it’s Ok, except that I am missing the XML declaration.
If I add <?xml version="1.0" encoding="UTF-8" ?> on top of Template/Layout/xml/xml.ctp
, I get a server 500 Error: PHP Parse error: syntax error, unexpected ‘version’ (T_STRING) in […] /src/Template/Layout/xml/xml.ctp.
Do I need a proper XML declaration for regular APIs?
Could anyone help me solve my proble?
Thanks for reading this, and helping).
Antonio