I'm following this tutorial for creating automated sitemaps in CakePHP. Everything is easy, but I'm not able 开发者_如何学编程to output XML. The controller looks like
function sitemap ()
{
Configure::write ('debug', 0);
$cats = $this->Category->find('all', array('fields' => array('nicename', 'modified')), null, -1);
$posts = $this->Post->find('all', array('fields' => array('name', 'modified')), null, -1);
$this->set(compact('cats','posts'));
$this->RequestHandler->respondAs('xml');
$this->viewPath .= '/xml';
$this->layoutPath = 'xml';
}
so it uses the layout app/views/layouts/xml/default.ctp
The problem is that I have no clue what to put in the layout file, and in the tutorial there is no indication. If I put
<?php
echo '<?xml version="1.0" encoding="UTF-8"?>'. "\n";
echo $content_for_layout;
?>
I obtain a file in the correct format, but as text. If I put
<?xml version="1.0" encoding="UTF-8"?>
<?php echo $content_for_layout; ?>
I obtain an XML file (it is displayed differently in Firefox) but completely empty, so the XML parser complains. What is the right way?
I just stumbled upon your question and was sad to see it unanswered. So let's give it a try.
In your controller you already have $this->RequestHandler->respondAs('xml');
so according to this comment, it should be plenty enough. Did you try leaving out the layout blank?
Also, you should have var $components = array('RequestHandler');
at some point either in your sitemap controller, or your app_controller.
精彩评论