首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >rss helper不会生成index.rss

rss helper不会生成index.rss
EN

Stack Overflow用户
提问于 2012-02-22 13:25:19
回答 2查看 563关注 0票数 0

我在尝试生成rss时遇到问题。我遵循了http://book.cakephp.org/1.3/en/view/1460/RSS的所有步骤,但是当我尝试输入url时,我的index.rss只显示了我的索引页,而不是xml格式。

这是我的post_Controller索引:

代码语言:javascript
复制
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中的布局:

代码语言:javascript
复制
 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中的视图

代码语言:javascript
复制
    $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相同

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-02-25 01:09:43

看起来您没有在索引控制器中返回RSS视图。更新index函数的RSS部分以返回rss视图:

代码语言:javascript
复制
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

票数 1
EN

Stack Overflow用户

发布于 2015-11-19 15:05:42

在控制器的作用下。您必须添加

代码语言:javascript
复制
 $this->response->type("xml");

在最后。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9389513

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档