This recipe shows how to get content from remote RSS feeds using a datasource.
We start by installing the fork of CakePHP's datasources plugin. Download the latest release from http://github.com/mariano/datasources/downloads and uncompress the downloaded file into your app/plugins
folder. You should now have a directory named datasources
. The fork used in this recipe uses a refactored version of the RSS datasource developed by Donatas Kairys, member of Loadsys Consulting. This modified version improves the datasource performance, and adds the possibility of changing the feed URL through a find setting. More information about the original datasource can be obtained at http://blog.loadsys.com/2009/06/19/cakephp-rss-feed-datasource.
app/config/database.php
file and add the following connection:public $feed = array( 'datasource' => 'datasources.RssSource', 'url' => 'http://marianoiglesias.com.ar/category/cakephp/feed/' );
Post
in a file named post.php
and place it in your app/models
folder with the following contents:<?php class Post extends AppModel { public $useDbConfig = 'feed'; } ?>
posts_controller.php
and place it in your app/controllers
folder with the following contents:<?php class PostsController extends AppController { public $helpers = array('Time'), public function index() { $this->paginate = array( 'order' => array('pubDate' => 'desc'), 'limit' => 9 ); $this->set('posts', $this->paginate()); } } ?>
posts
in your app/views
folder, and in that folder create a file named index.ctp
with the following contents:<p> <?php echo $this->Paginator->prev(); ?> <?php echo $this->Paginator->numbers(); ?> <?php echo $this->Paginator->next(); ?> </p> <table> <thead><tr><th>Title</th><th>Published</th></tr></thead> <tbody> <?php foreach($posts as $post) { ?> <tr> <td><?php echo $this->Html->link($post['Post']['title'], $post['Post']['link']); ?></td> <td><?php echo $this->Time->nice($post['Post']['pubDate']); ?></td> </tr> <?php } ?> </tbody> </table>
If we now browse to http://localhost/posts
, we should see a paginated list of posts, as shown in the following screenshot:
We start by creating a new connection named feed
, specifying datasources.FeedSource
as its type. We use the setting url
to specify the address of the feed source. Among other available connection settings we have:
encoding
: Sets the character encoding to use. Defaults to the CakePHP App.encoding
configuration setting.cache
: If set to false
, no caching will be done. Otherwise this is the cache configuration name to use. Defaults to the configuration named default
.We then create the Post
model, specifying its underlying connection to be feed
through the useDbConfig
property. We then proceed to setup a paginated list of posts sorting by publication date (pubDate field) in descending order, and limiting to nine posts per page.
Just as with the CSV datasource shown in the recipe, Parsing CSV files with a datasource, the RSS datasource allows some basic filtering. For example, to only show posts that were created in the year 2009 or later, we would add the following conditions
setting to our index()
method:
public function index()
{
$this->paginate = array(
'conditions' => array('pubDate >=' => '2009-01-01'),
'order' => array('pubDate' => 'desc'),
'limit' => 9
);
$this->set('posts', $this->paginate());
}
There are cases where we might not be able to define the feed URL in a configuration file, for example, if the URL comes from a dynamic data source. Fortunately, for these cases we have the option to define the feed address through a custom find setting.
In the above example, we could remove the feed URL from the connection settings, and specify it as a find setting named url
:
$this->paginate = array(
'url' => 'http://marianoiglesias.com.ar/category/cakephp/feed/',
'order' => array('pubDate' => 'desc'),
'limit' => 9
);
$this->set('posts', $this->paginate());
We've seen how we can change the feed URL by using a custom find setting. However, we could also change this address by modifying the connection settings. Using the method setConfig()
, available in all datasources, we can make changes to any connection setting. For example, instead of using the url
custom find setting, we'll change the feed URL by changing the connection:
$this->Post->getDataSource()->setConfig(array(
'url' => 'http://marianoiglesias.com.ar/category/cakephp/feed/'
));
$this->paginate = array(
'order' => array('pubDate' => 'desc'),
'limit' => 9
);
$this->set('posts', $this->paginate());
18.188.139.172