LastRSS CakePHP Component

By Jimmy aka "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:

Download code <?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:

Download code <?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:

Download code
   <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>


Comments 638

CakePHP team comments Author comments

Comment

1 1.2

Seems to work just fine in 1.2, fyi.
posted Wed, Mar 19th 2008, 23:15 by George Walker

Bug

2 Small bug

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
posted Thu, Mar 27th 2008, 08:16 by Finjon Kiang

Comment

3 Cannot Redeclare lastrssComponent

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:ApachehtdocsfeedreadervendorslastRSSCakelastrss.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?
posted Fri, Apr 4th 2008, 08:49 by Keith M.

Comment

4 View

What should I call the view and how do I browse to it? Im trying to learn cakephp.
posted Sun, Apr 13th 2008, 15:15 by Jake Rutter

Login to Submit a Comment