我正在关注this tutorial用于在 CakePHP 中创建自动站点地图。一切都很简单,但我无法输出 XML。 Controller 看起来像
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';
}
因此它使用布局 app/views/layouts/xml/default.ctp
问题是我不知道要在布局文件中放入什么,并且在教程中没有任何指示。如果我放
<?php
echo '<?xml version="1.0" encoding="UTF-8"?>'. "\n";
echo $content_for_layout;
?>
我获得了格式正确的文本文件。如果我放
<?xml version="1.0" encoding="UTF-8"?>
<?php echo $content_for_layout; ?>
我获得了一个 XML 文件(它在 Firefox 中的显示方式有所不同),但完全是空的,因此 XML 解析器会提示。正确的做法是什么?
最佳答案
我刚刚偶然发现了你的问题,很遗憾看到它没有得到解答。那么让我们尝试一下吧。
在你的 Controller 中,你已经有 $this->RequestHandler->respondAs('xml');
所以根据 this comment ,应该足够了。您是否尝试将布局留空?
此外,您的站点地图 Controller 或 app_controller 中的某个时刻应该有 var $components = array('RequestHandler');
。
关于php - CakePHP 中的自动站点地图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2246353/