LastRSS CakePHP Component
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
- CakePHP either 1.1 - I have not tested on 1.2
- Download LastRSS (http://lastrss.oslab.net/) and unzip the contents into the vendors folder and rename the file to 'lastrss.php' (all lowercase).
- Download the component (or paste the full code from below) and put it in 'app/controllers/components/lastrss.php'.
- 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>

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)
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.
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?
} else {replace with}} else {2. In LastrssComponent
vendor('lastrss');must be replaced withApp::import('Vendor', 'lastrss');When using CakePHP 1.2