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 amRSS Feeds are really very helpful and you could get site and news updates from it..;’
May 20th, 2010 at 11:35 am[...] CakePHP RSS Feed Datasource [...]
June 26th, 2010 at 8:02 amRSS feeds are really great because you are always updated with the latest news or blog posts.’;-
July 28th, 2010 at 2:24 amNice work, it’s helpful.
Thanks
September 12th, 2010 at 4:00 amPlease could you describe the exact names for each file in the example. The probe and does not work, and not to put names to each file.
Thanks you
September 14th, 2010 at 10:39 amWarning (2): Parameter 1 to array_multisort() expected to be a reference, value given [APP/models/datasources/rss_source.php, line 179]
Help me please!
September 14th, 2010 at 2:44 pmOn line 82 and 85 there’s a function cache($cachePath, null, $cacheTime) and cache($cachePath, serialize($data)) that I can’t seem to find documentation on anywhere. Where are the functions documented? As far as I can tell, they’re not global CakePHP functions or regular PHP functions. It’s difficult to google for “php cache,” so I may be wrong.
November 21st, 2010 at 12:55 pmThird time’s the charm! It’s officially deprecated, and it only took an hour and a half of searching the much acclaimed CakePHP documentation: http://api13.cakephp.org/file/cake/basics.php#function-cache – you’re supposed to use Cache::write() instead.
November 21st, 2010 at 1:32 pm[...] google, question Hi, I am new here, I ve done everything in this tutorial [link] , Im healthy to read RSS Feeds from external site, Can I save apiece feed in my local database? [...]
December 4th, 2010 at 5:44 pmGetting following error.
Error: Database table google_news for model GoogleNews was not found.
December 22nd, 2010 at 3:51 amHi,
How can i dynamically change the feed url, depending on the predefined categories. For example the rss feed news is broken into months, so when a user clicks on a month the url should change to “?month=1″,
var $googleNews = array(
‘datasource’ => ‘rss’,
‘feedUrl’ => ‘http://news.google.com/news?pz=1&ned=us&hl=en&output=rss?month=1‘,
‘encoding’ => ‘UTF-8′,
‘cacheTime’ => ‘+1 day’,
);
I also need pagination working so how can I achieve this.
Thanks
January 16th, 2011 at 5:53 pmSnct2
Cakephp datasource for valves steam api
The steam api is something i’ve been working with recently as i occasionally do projects for a gaming community. If you don’t know what steam is its basically a piece of software that allows you to download games, play them, chat with frien…
April 5th, 2011 at 5:55 amThank you for writing this article. I really enjoyed it. I work in web design too. Keep up the excellent work!
May 26th, 2011 at 1:31 pmRe: “table not found”, make sure the model file name corresponds to the datasource name. So e.g., if the datasource is googleNews, you need a app/models/google_news.php model file (see filenaming conventions in cookbook for more details).
jd
August 18th, 2011 at 11:23 am