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

17 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


Leave a Reply

Fill out the following to submit a comment.





Close
E-mail It