Hello folks.
I followed the manual description for XML Views to generate a sitemap.xml
But I get an wrong content-type error:
Incorrect http header content-type: “text/html; charset=UTF-8” (expected: “application/xml”)
This is my Controller:
<?php
declare(strict_types=1);
namespace App\Controller;
use Cake\Routing\Router;
use Cake\View\XmlView;
use Cake\I18n\DateTime;
/**
* Sitemap Controller
*/
class SitemapController extends AppController
{
/**
* @param \Cake\Event\EventInterface $event
* @return \Cake\Http\Response|void|null
*/
public function beforeFilter(\Cake\Event\EventInterface $event)
{
parent::beforeFilter($event);
$this->Authentication->addUnauthenticatedActions(['sitemap']);
}
public function viewClasses(): array
{
return [XmlView::class];
}
/**
* Provides a sitemap as an XML document, following the sitemap protocol.
*
* @link https://www.sitemaps.org/protocol.html
*/
public function sitemap()
{
$pages = $this->fetchTable('Pages')->find('all', fields: ['id', 'slug', 'modified'], order: ['pages_order' => 'ASC']);
foreach ($pages as $entry) {
$urls[] = [
'loc' => Router::url(['controller' => 'Pages', 'action' => 'view', 'id' => $entry->id, 'slug' => $entry->slug, '_full' => true]),
'lastmod' => $entry->modified->format('Y-m-d'),
'changefreq' => 'weekly',
'priority' => '0.5',
];
}
// Define a custom root node in the generated document.
$this->viewBuilder()
->setOption('rootNode', 'urlset')
->setOption('serialize', ['@xmlns', 'url']);
$this->set([
// Define an attribute on the root node.
'@xmlns' => 'http://www.sitemaps.org/schemas/sitemap/0.9',
'url' => $urls,
]);
}
}
What am I missing?