我在尝试生成rss时遇到问题。我遵循了http://book.cakephp.org/1.3/en/view/1460/RSS的所有步骤,但是当我尝试输入url时,我的index.rss只显示了我的索引页,而不是xml格式。
这是我的post_Controller索引:
var $components = array('Session','RequestHandler');
var $helpers = array('Html','Form','Time','Text');
function index() {
if( $this->RequestHandler->isRss() ){
$posts = $this->Post->find('all', array('limit' => 20, 'order' => 'Post.created DESC'));
$this->set(compact('posts'));
}
$this->set('title_for_layout', 'mi blog');
$this->Post->recursive = 1;
$this->set('posts', $this->paginate());
}这是我在app/views/ layout /rss/default.ctp中的布局:
echo $this->Rss->header();
if (!isset($documentData)) {
$documentData = array();
}
if (!isset($channelData)) {
$channelData = array();
}
if (!isset($channelData['title'])) {
$channelData['title'] = $title_for_layout;
}
$channel = $this->Rss->channel(array(), $channelData, $content_for_layout);
echo $this->Rss->document($documentData,$channel);这是app/views/post/rss/index.ctp中的视图
$this->set('documentData', array(
'xmlns:dc' => 'http://purl.org/dc/elements/1.1/'));
$this->set('channelData', array(
'title' => __("Articles", true),
'link' => $this->Html->url('/', true),
'description' => __("Articulos mas recientes.", true),
'language' => 'en-us'));
// content
foreach ($posts as $post) {
$postTime = strtotime($post['Post']['created']);
$postLink = array(
'controller' => 'posts',
'action' => 'view',
$post['Post']['id']);
// You should import Sanitize
App::import('Sanitize');
// This is the part where we clean the body text for output as the description
// of the rss item, this needs to have only text to make sure the feed validates
$bodyText = preg_replace('=\(.*?\)=is', '', $post['Post']['body']);
$bodyText = $this->Text->stripLinks($bodyText);
$bodyText = Sanitize::stripAll($bodyText);
$bodyText = $this->Text->truncate($bodyText, 400, array(
'ending' => '...',
'exact' => true,
'html' => true,
));
echo $this->Rss->item(array(), array(
'title' => $post['Post']['title'],
'link' => $postLink,
'guid' => array('url' => $postLink, 'isPermaLink' => 'true'),
'description' => $bodyText,
'pubDate' => $post['Post']['created']));
}这可能就是问题所在。我还将组件放入了数组var $components = app_controller.php ('Auth','Session',‘RequestHandler’)中;但是没有发生任何事情,index.rss与posts / index相同
发布于 2012-02-25 01:09:43
看起来您没有在索引控制器中返回RSS视图。更新index函数的RSS部分以返回rss视图:
function index() {
if( $this->RequestHandler->isRss() ){
$posts = $this->Post->find('all', array('limit' => 20, 'order' => 'Post.created DESC'));
return $this->set(compact('posts'));
}
// ...snip...
}更新
这就是Chrome处理布局的方式。我知道这很可怕。FireFox和IE处理RSS布局要好得多。但是你可以安装Chrome的RSS布局扩展,它会以同样的方式格式化它。
https://chrome.google.com/extensions/detail/nlbjncdgjeocebhnmkbbbdekmmmcbfjd
发布于 2015-11-19 15:05:42
在控制器的作用下。您必须添加
$this->response->type("xml");在最后。
https://stackoverflow.com/questions/9389513
复制相似问题