LastRSS CakePHP Component

by jimmygle
LastRSS is a PHP class for retrieval and parsing of RSS feeds. This is a wrapper to that class making it easy to use in the CakePHP framwork. Much of this component is taken from the work of Matt Curry (http://bakery.cakephp.org/articles/view/simplepie-cakephp-component) and Scott Sansoni (http://cakeforge.org/snippet/detail.php?type=snippet&id=53).

Download

You can download the required LastRSS files at http://lastrss.oslab.net/

Usage

  1. CakePHP either 1.1 - I have not tested on 1.2
  2. Download LastRSS (http://lastrss.oslab.net/) and unzip the contents into the vendors folder and rename the file to 'lastrss.php' (all lowercase).
  3. Download the component (or paste the full code from below) and put it in 'app/controllers/components/lastrss.php'.
  4. Include the component in any controller that will need it.

Component Class:

<?php 
/*
 * LastRSS CakePHP Component
 * Copyright (c) 2007 Jimmy Gleason
 * www.jimmygleason.com
 *
 * Based on the work of Matt Curry (http://bakery.cakephp.org/articles/view/simplepie-cakephp-component) &
 * Based on the work of Scott Sansoni (http://cakeforge.org/snippet/detail.php?type=snippet&id=53) 
 *
 * @author Jimmy Gleason
 * @version 1.0
 * @license MIT
 *
 */  

class LastrssComponent extends Object {
   var 
$cache;

   function 
__construct() {
      
$this->cache CACHE 'rss' DS
   }

   function 
feed($feed_url) {

      
// Make the cache dir if it doesn't exist
      
if (!file_exists($this->cache)) {
         
uses('folder');
         
$folder = new Folder();
         
$folder->mkdirr($this->cache);
      }

      
// Include the vendor class
      
vendor('lastrss');

      
// Setup LastRSS
      
$feed = new lastRSS();
      
$feed->cache_dir $this->cache;
      
$feed->cache_time 3600// one hour

      // Load RSS file
      
if($rss $feed->get($feed_url)) {
         
$items $rss;
         return 
$items;
      } else {
         
// Return false
         
return false;
      }
   }
}
?>

Controller Class:

<?php 
class FeedsController extends AppController {

   var 
$name 'Feeds';
   var 
$components = array('Lastrss');

   function 
showFeed() {

      
$items $this->Lastrss->feed('http://rss.news.yahoo.com/rss/topstories'); // Get feed
      
$this->set("items",$items); // Send feed to view
   
}

}
?>

View Template:


   <ul class="rssfeed">
      <?php if($items) { foreach($items['items'] as $item) { ?>
         <li class="rss"><?php echo $html->link($item['title'],$item['link']); ?></li>
      <?php } else { ?>
         <li class="rsserror">Feed Error</li>
      <?php ?>
   </ul>

Report

More on Components

Advertising

Comments

  • BryceB posted on 05/18/11 07:24:33 PM
    Here's what I did to get it to work.

    In the Component class I changed:

    $folder->mkdirr($this->cache);
    to
    $folder->create($this->cache);

    and changed
    vendor('lastrss');
    to
    App::import('Vendor', 'lastrss');


    In my controller I changed: ( I just wanted the Items, not the feed metadata)
    $this->set("items",$items);
    to
    $this->set("items",$items['items']); // Send feed to view

    And in the view I wrapped any text I'm using like this
    htmlspecialchars_decode($item['title'],ENT_QUOTES)
  • donesleh posted on 03/02/11 08:55:14 AM
    does it ork with cakephp 1.3 ??
  • admun posted on 01/11/10 05:16:37 PM
    It's a typo. The line should read: $folder->mkdir($this->cache);
  • Sharma posted on 03/23/09 06:47:38 PM
    I know someone mentioned that it works fine in 1.2, but it was complaining for me on the mkdirr() function:


          if (!file_exists($this->cache)) {
             uses('folder');
             $folder = new Folder();
             $folder->mkdirr($this->cache);
          } 

    I changed that to just $folder->create($this->cache) since the create method will recursive directories now.
  • jrutter posted on 04/13/08 03:15:56 PM
    What should I call the view and how do I browse to it? Im trying to learn cakephp.
  • kmedlinnc posted on 04/04/08 08:49:15 AM
    Since it appears others have gotten this to work any idea what I'm doing wrong to get the error: Fatal error: Cannot redeclare class lastrssComponent in C:\Apache\htdocs\feedreader\vendors\lastRSSCake\lastrss.php on line 242

    Line 242 in lastrss.php is the final line of the class. So it's after the file has been included by the component. I tried renaming classes, etc. and then LastRSS cannot instantiate.

    I'm sure it's something small I'm overlooking. Any thoughts?
  • kiang posted on 03/27/08 08:16:24 AM
    1. In the View Template
    } else { replace with
    }} else {
    2. In LastrssComponent
    vendor('lastrss'); must be replaced with
    App::import('Vendor', 'lastrss'); When using CakePHP 1.2
  • Finster posted on 03/19/08 11:15:57 PM
    Seems to work just fine in 1.2, fyi.
login to post a comment.