php - CakePHP 中的自动站点地图

标签 php xml cakephp sitemap

我正在关注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/

相关文章:

PHP SimpleXML : How to access nested namespaces?

html - 修复 Visual Studio 中 aspx 文件的格式?

java - xsd中的限制元素

php - CakePHP 使用选择条件从多个表中检索数据

php - 加载 xdebug 时 CakePHP 命令行不工作

php - 使用 cakephp 按日期分组

php - Mysql 和 PHP 字符串比较 - Unicode 规范化?

php - github API v3 - 通过 CURL 获取访问 token

javascript - 根据另一列计算列 laravel

java - 在 android 中设置 TextView 的可见性不起作用