【问题标题】:the rss helper doesnt generate the index.rssrss 助手不会生成 index.rss
【发布时间】:2012-02-22 05:25:19
【问题描述】:

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

【问题讨论】:

  • 记得把这段代码添加到config/routes.php Router::parseExtensions('rss');
  • 是的,我已经添加了..
  • 我解决了这个问题...但是为什么在 chrome 中 index.rss 看起来像这样... purl.org/dc/elements/1.1" version="2.0">latest posts......在firefox中看起来不错??
  • 我在 chrome 中遇到了同样的问题。

标签: cakephp components cakephp-1.3


【解决方案1】:

看起来您没有在索引控制器中返回 RSS 视图。更新 index 函数的 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 布局扩展,它会以同样的方式格式化它。

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

【讨论】:

  • 我想念那个回报......但它同样有效,现在为什么在 chrome 中我只看到 xml 格式
  • 这是 chrome 处理布局的方式。在我的回答中查看我关于如何处理它的更新。
【解决方案2】:

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

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

在最后。

【讨论】:

    猜你喜欢
    • 2011-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多