[Solved] Returning XLM on a view

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:

<?php foreach($news as $news): ?> <?php $title = strip_tags($news->title); $content = strip_tags($news->content); ?> <?= $title ?> <?= $content ?> <?= $news->date->i18nFormat('YYYY-MM-dd'); ?> <?php endforeach; ?>

In Template/Layout/xml/xml.ctp:

<?= $this->fetch('content') ?>

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

Hi,
Maybe you could try by putting this in your layout xml.ctp:

<?= '<?xml version="1.0" encoding="UTF-8" ?>' ?>

This worked!
Thank you!

Sorry, why <?= '<?xml version="1.0" encoding="UTF-8" ?>' ?> and not simply <?xml version="1.0" encoding="UTF-8" ?>?