CakePHP RSS Feed Datasource
June 19th, 2009We are releasing our RSS datasource to CakePHP developers for CakePHP Development. We know there are others out there, but what we like about ours is that it supports the following:
- Built-in Pagination
- Sorting
- Content filtering
Download rss_source.php here
Steps to using this datasource:
- Copy rss_source.php to models/datasources
- Create config for a feed in config/database.php
var $googleNews = array( 'datasource' => 'rss', 'feedUrl' => 'http://news.google.com/news?pz=1&ned=us&hl=en&output=rss‘, ‘encoding’ => ‘UTF-8′, ‘cacheTime’ => ‘+1 day’, );
- Create model for the feed
<?php class GoogleNews extends AppModel {
var $useTable = false;
var $useDbConfig = 'googleNews';
}
?>
- In a controller just do $this->GoogleNews->find(’all’) or even use paginate. Conditions are specified as regular expressions.
class NewsController extends AppController {
var $name = 'News';
var $uses = array('GoogleNews');
function index() {
$this->paginate = array(
'limit' => 2,
'conditions' => array(
'title' => '/(Obama)+/',
),
'order' => array(
'GoogleNews.pubDate' => 'desc',
),
);
$this->set('news', $this->paginate('GoogleNews') );
}
}
- Here is a sample view
<?php foreach( $news as $newsItem ) : ?>
<?php echo $html->link($newsItem['GoogleNews']['title'], $newsItem['GoogleNews']['link']); ?><br/>
<em><?php echo $newsItem['GoogleNews']['pubDate']; ?></em>
<hr>
<?php endforeach; ?>
<div class="paging">
<?php echo $paginator->prev('<< '.__('previous', true), array(), null, array('class'=>'disabled'));?>
| <?php echo $paginator->numbers();?>
<?php echo $paginator->next(__('next', true).' >>', array(), null, array('class'=>'disabled'));?>
</div>
And there you have it. Easy RSS via CakePHP.
Share This