我有一個問題,試圖生成rss。我遵循http://book.cakephp.org/1.3/en/view/1460/RSS的所有步驟,但是當我嘗試在網址中我的index.rss只顯示我的索引頁不是xml格式。rss幫手不生成index.rss
這是我的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());
}
這是我在應用程序/視圖/佈局/ RSS/default.thtml中佈局:
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視圖/視圖/文章/ 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']));
}
這可能是......我也有把組件的app_controller.php問題var $ components = array('Auth','Session','RequestHandler'); 但沒有任何反應index.rss是相同的帖子/索引
記得將此代碼添加到config/routes.php Router :: parseExtensions('rss'); – 2012-02-22 08:37:58
是的,我已經添加了它.. – Leoh 2012-02-23 01:09:21
我解決了這個問題......但爲什麼在鉻的index.rss看起來像這樣... <?xml version =「1.0」encoding =「UTF-8」?>最新帖子 ......並在firefox看起來不錯? –
Leoh
2012-02-23 19:10:40