rss helper不会生成index.rss

时间:2012-02-22 05:25:19

标签: cakephp components cakephp-1.3

我在尝试生成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 / layouts / 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 / posts / 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是相同的帖子/索引

2 个答案:

答案 0 :(得分: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...
}

<强>更新

这是Chrome处理布局的方式。我知道这太可怕了。 FireFox和IE处理RSS布局要好得多。但您可以为Chrome安装RSS Layout扩展程序,它将以相同的方式对其进行格式化。

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

答案 1 :(得分:1)

在控制器的动作中。你必须添加

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

最后。