CakePhP 3: Building a XML sitemap

Hi All

I’m building a XML sitemap myself. I know there are plugins available, but I need to learn about XML and CakePHP for later in my project so thought this could be a good training exercise.

I have a problem. If I use the following in my controller the Browser recognises the file as an XML file however if does not use my sitemap.ctp file and outputs all of the data.

	$this->RequestHandler->renderAs($this, 'xml');

But if I use the following it does output the data exactly, but my browser does not think the file is an XML file.

	$this->RequestHandler->respondAs('xml', [
		'charset' => 'UTF-8'

I am using a sitemap specific layout;

Where am I going wrong?

Thanks in advance.


I think you are missing the router extensions


As @raul338 mentioned, you might want to look at using routing file extensions. When combined RequestHandler, you should end up having Templates/Sitemaps/xml/sitemap.ctp rendered (assuming your controller is Sitemaps. Inside that ctp you can build out your XML.

Many thanks for Raul and Mark’s replies.

I have added the following to my routes.php
$routes->connect(’/sitemap’, [‘controller’ => ‘manufacturers’, ‘action’ => ‘sitemap’]);

But this now behaves when renderAs is used - ie not use my sitemap.ctp file.

Anybody got any further ideas?