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.
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 🙂
Oops. Thanks Carlos. Definitely a typo. Thanks for the feedback.
Looks 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
Looks pretty good, but you have your file header above the first <?php
@Mark: Thanks, sorry missed that before uploading it.
The new download now has the header in the correct place.
you 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 😉
Very nice slice o’ cake! It throws some warnings when there’s no match found though:
Notice (8): Undefined variable: result [APPmodelsdatasourcesrss_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!
Очень восхитила ваша статья, в ней спрятано очень многонько поднято с нашей общечеловеческой повседневной жизни и это аж хорошо! Есть над чем призадуматься
Is there any datasource for amazon simpleDB for cakePhp??
каталог rss
Have 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.
I’ve figured out how to make it sortable – see my blog post at http://www.willis-owen.co.uk/wp/?p=81
RSS Feeds are really very helpful and you could get site and news updates from it..;’
RSS feeds are really great because you are always updated with the latest news or blog posts.’;-
Nice work, it’s helpful.
Thanks
Please 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
Warning (2): Parameter 1 to array_multisort() expected to be a reference, value given [APP/models/datasources/rss_source.php, line 179]
Help me please!
On 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.
Third 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.
Getting following error.
Error: Database table google_news for model GoogleNews was not found.
Hi,
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
Snct2
Thank you for writing this article. I really enjoyed it. I work in web design too. Keep up the excellent work!
Re: “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