Loadsys

LoadStr – A Loadsys Blog

A Web Development Company Specializing In Standard-Based Web Design and the CakePHP Framework.

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 This

Tags: , , , ,

31 Responses to “CakePHP RSS Feed Datasource”

  1. Carlos Pires Says:

    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 :)


  2. Lee Says:

    Oops. Thanks Carlos. Definitely a typo. Thanks for the feedback.


  3. sam Says:

    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


  4. Mark Story Says:

    Looks pretty good, but you have your file header above the first <?php


  5. CakePHP : signets remarquables du 14/06/2009 au 20/06/2009 | Cherry on the... Says:

    [...] CakePHP RSS Feed Datasource [...]


  6. Lee Says:

    @Mark: Thanks, sorry missed that before uploading it.

    The new download now has the header in the correct place.


  7. julien Says:

    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 ;)


  8. Dan Says:

    Very 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!


  9. Jamie Nay » Useful CakePHP Tutorial Roundup for July 03, 2009 Says:

    [...] 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… [...]


  10. CakePHP Digest #17 – Non-CakeFest Edition | PseudoCoder.com Says:

    [...] nice piece of code from the Loadsys [...]


  11. [PRONIQUE] CakePHP Developer Links Says:

    [...] http://blog.loadsys.com/…19/cakephp-rss-feed-datasource/ [...]


  12. Апполинарий Says:

    Очень восхитила ваша статья, в ней спрятано очень многонько поднято с нашей общечеловеческой повседневной жизни и это аж хорошо! Есть над чем призадуматься


  13. Tuhin Says:

    Is there any datasource for amazon simpleDB for cakePhp??


  14. каталог rss Says:

    каталог rss


  15. Zend_Search_Lucene Datasource for CakePHP | Jamie Nay Says:

    [...] used the RSS Feed datasource by Loadsys as a guide to good datasource design. I may have borrowed a function or [...]


  16. Richard Willis-Owen Says:

    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.


  17. Richard Willis-Owen Says:

    I’ve figured out how to make it sortable – see my blog post at http://www.willis-owen.co.uk/wp/?p=81


  18. Zachary Graham Says:

    RSS Feeds are really very helpful and you could get site and news updates from it..;’


  19. 9 Awesome CakePHP Tutorials | DevMoose Says:

    [...] CakePHP RSS Feed Datasource [...]


  20. Daniel Davis Says:

    RSS feeds are really great because you are always updated with the latest news or blog posts.’;-


  21. raultm Says:

    Nice work, it’s helpful.

    Thanks


  22. Alan Says:

    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


  23. Alan Says:

    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!


  24. Myer Nore Says:

    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.


  25. Myer Nore Says:

    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.


  26. Reading External Data and Saving it to database | DeveloperQuestion.com Says:

    [...] 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? [...]


  27. Priyank Says:

    Getting following error.

    Error: Database table google_news for model GoogleNews was not found.


  28. Shou Says:

    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


  29. David E Jones Says:

    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…


  30. Max D Says:

    Thank you for writing this article. I really enjoyed it. I work in web design too. Keep up the excellent work!


  31. Jim D Says:

    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


Leave a Reply

Fill out the following to submit a comment.





Close
E-mail It