In a CakePHP project, I needed to extract an XML string from the database and display it as an XML page. The docs suggested this would be easy with the XmlView, but I struggled to get it to work. However, after a fair amount of fiddling around, I found an easy way to display an XML view.
Here’s what I had to do:
- Enable parsing of .xml extensions in routes.php:
Router::parseExtensions('xml');
- Set the viewClass to Xml in the controller:
$this->viewClass = 'Xml';
- Create a simple xml view in Views/YourController/xml/xml.ctp, and echo $xml in this view:
<?php echo $xml; ?>
- Back in the controller, set the XML string to the view:
$this->set('xml', $xmlString);
- Finally, render using the xml view you have just created (this must be after the ‘set’ line above):
$this->render('xml');
Unfortunately, this does not take advantage of the _serialize key, which would mean you don’t need to create a view file at all, but I just couldn’t get this to work with an XML string, despite trying various combinations of CakePHP’s Xml Class functions.