CakePHP RSS Feed Datasource
We 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 ThisTags: CakePHP Developers, cakephp development, cakephp rss feed, cakephp rss feed datasource, chicago cakephp
Hello guys!
I have only one tip: According to the Cake conventions, the name of the class model should be GoogleNews and not GoogleNewsModel.
Thank you for this great piece of cake
June 19th, 2009 at 1:34 pmOops. Thanks Carlos. Definitely a typo. Thanks for the feedback.
June 19th, 2009 at 1:48 pmLooks good. A Very nice idea. Do you know if dbconfigs can be built on the fly (I am sure I sure this elsewhere but can’t find it now) it would be really nice to load in an array of feed sources at runtime and display those
June 19th, 2009 at 9:04 pmLooks pretty good, but you have your file header above the first <?php
June 19th, 2009 at 10:23 pm[…] CakePHP RSS Feed Datasource […]
June 20th, 2009 at 3:02 am@Mark: Thanks, sorry missed that before uploading it.
The new download now has the header in the correct place.
June 20th, 2009 at 4:23 amyou should probably change the condition to match something else than obama since there’s less news about him on Google news. Thanks for the datasource, though
June 21st, 2009 at 7:24 pmVery nice slice o’ cake! It throws some warnings when there’s no match found though:
Notice (8): Undefined variable: result [APP\models\datasources\rss_source.php, line 69]
So could I suggest making it return “no results found” or an is_set condition or something? Other than that - beautiful work!
June 28th, 2009 at 6:09 am[…] CakePHP RSS Feed Datasource (June 19, 2009) The guys at Loadsys have released a nice datasource for RSS feeds that allows models to use RSS feeds as a source for data instead of a database. Great for displaying, say, the latest articles from an affiliate site or the day’s top news stories, especially since you treat the feed as a standalone model rather than as part of, for example, a controller component. Its only downfall, which is really the downfall of CakePHP datasources in general, is that you need to declare a separate config array for each feed in config/database.php. I would love to be able to point to an RSS feed dynamically, but… […]
July 3rd, 2009 at 9:50 am[…] nice piece of code from the Loadsys […]
July 13th, 2009 at 9:06 pm[…] http://blog.loadsys.com/…19/cakephp-rss-feed-datasource/ […]
July 23rd, 2009 at 10:11 amОчень восхитила ваша статья, в ней спрятано очень многонько поднято с нашей общечеловеческой повседневной жизни и это аж хорошо! Есть над чем призадуматься
August 1st, 2009 at 1:59 amIs there any datasource for amazon simpleDB for cakePhp??
August 20th, 2009 at 11:45 pmкаталог rss
December 3rd, 2009 at 1:12 am[…] used the RSS Feed datasource by Loadsys as a guide to good datasource design. I may have borrowed a function or […]
January 13th, 2010 at 3:53 pmHave you any suggestions on making this sortable via the paginator helper functions in the view e.g. $paginator->sort().
Currently it looks like the only way to sort is to hardcode it in the controller.
February 11th, 2010 at 7:15 amI’ve figured out how to make it sortable - see my blog post at http://www.willis-owen.co.uk/wp/?p=81
February 12th, 2010 at 4:51 am