2012-02-22 49 views
0

我有一個問題,試圖生成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是相同的帖子/索引

+0

記得將此代碼添加到config/routes.php Router :: parseExtensions('rss'); – 2012-02-22 08:37:58

+0

是的,我已經添加了它.. – Leoh 2012-02-23 01:09:21

+0

我解決了這個問題......但爲什麼在鉻的index.rss看起來像這樣... <?xml version =「1.0」encoding =「UTF-8」?>最新帖子 ......並在firefox看起來不錯? – Leoh 2012-02-23 19:10:40

回答

1

它看起來像你沒有返回在索引控制器的RSS視圖。更新索引功能的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... 
} 

UPDATE

這是Chrome瀏覽器如何處理佈局。我知道這太可怕了。 FireFox和IE處理RSS佈局好得多。但是,您可以安裝Chrome的RSS佈局擴展程序,並以相同的方式對其進行格式化。

https://chrome.google.com/extensions/detail/nlbjncdgjeocebhnmkbbbdekmmmcbfjd

+0

我想念那個回報...但它同樣的工作,現在爲什麼在鉻只是我看到的XML格式 – Leoh 2012-02-24 23:43:25

+0

這是鉻處理佈局的方式。在我的答案中查看我的更新,瞭解如何處理它。 – 2012-02-25 01:49:28

+0

好吧.. – Leoh 2012-02-26 19:48:11

1

在控制器的動作。您必須添加

$this->response->type("xml"); 

最後。