2012-02-22 51 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"); 

最后。