2019-11-18 05:58:33 +01:00
---
2014-09-28 05:59:48 +02:00
title: RSS Feed
summary: Output records from your database as an RSS Feed.
2019-11-18 05:58:33 +01:00
icon: rss
---
2011-02-07 07:48:44 +01:00
2014-09-28 05:59:48 +02:00
# RSS Feed
2011-02-07 07:48:44 +01:00
2017-07-03 03:22:12 +02:00
Generating RSS / Atom-feeds is a matter of rendering a [SS_List ](api:SilverStripe\ORM\SS_List ) instance through the [RSSFeed ](api:SilverStripe\Control\RSS\RSSFeed ) class.
2011-02-07 07:48:44 +01:00
2017-07-03 03:22:12 +02:00
The [RSSFeed ](api:SilverStripe\Control\RSS\RSSFeed ) class doesn't limit you to generating article based feeds, it is just as easy to create a feed of
your current staff members, comments or any other custom [DataObject ](api:SilverStripe\ORM\DataObject ) subclasses you have defined. The only
2014-09-28 05:59:48 +02:00
logical limitation here is that every item in the RSS-feed should be accessible through a URL on your website, so it's
2017-07-03 03:22:12 +02:00
advisable to just create feeds from subclasses of [SiteTree ](api:SilverStripe\CMS\Model\SiteTree ).
2012-09-18 09:42:21 +02:00
2019-11-18 05:58:33 +01:00
[warning]
2017-07-03 03:22:12 +02:00
If you wish to generate an RSS feed that contains a [DataObject ](api:SilverStripe\ORM\DataObject ), ensure you define a `AbsoluteLink` method on
2014-09-28 05:59:48 +02:00
the object.
2019-11-18 05:58:33 +01:00
[/warning]
2011-02-07 07:48:44 +01:00
## Usage
2014-09-28 05:59:48 +02:00
Including an RSS feed has two steps. First, a `Controller` action which responses with the `XML` and secondly, the other
web pages need to link to the URL to notify users that the RSS feed is available and where it is.
2012-09-18 09:42:21 +02:00
2014-09-28 05:59:48 +02:00
An outline of step one looks like:
2012-09-18 09:42:21 +02:00
2017-08-03 02:51:32 +02:00
```php
2017-10-27 04:38:27 +02:00
use SilverStripe\Control\RSS\RSSFeed;
$feed = new RSSFeed(
$list,
$link,
$title,
$description,
$titleField,
$descriptionField,
$authorField,
$lastModifiedTime,
$etag
);
$feed->outputToBrowser();
2017-08-03 02:51:32 +02:00
```
2011-02-07 07:48:44 +01:00
2014-09-28 05:59:48 +02:00
To achieve step two include the following code where ever you want to include the `<link>` tag to the RSS Feed. This
will normally go in your `Controllers` `init` method.
2017-10-27 04:38:27 +02:00
2017-08-03 02:51:32 +02:00
```php
2017-10-27 04:38:27 +02:00
RSSFeed::linkToFeed($link, $title);
2017-08-03 02:51:32 +02:00
```
2014-09-28 05:59:48 +02:00
## Examples
2011-02-07 07:48:44 +01:00
2012-09-18 09:42:21 +02:00
### Showing the 10 most recently updated pages
2011-02-07 07:48:44 +01:00
2017-07-03 03:22:12 +02:00
You can use [RSSFeed ](api:SilverStripe\Control\RSS\RSSFeed ) to easily create a feed showing your latest Page updates. The following example adds a page
2014-09-28 05:59:48 +02:00
`/home/rss/` which displays an XML file the latest updated pages.
2019-03-23 14:22:37 +01:00
**app/code/PageController.php**
2011-02-07 07:48:44 +01:00
2017-08-03 02:51:32 +02:00
```php
2017-10-27 04:38:27 +02:00
use SilverStripe\Control\RSS\RSSFeed;
use SilverStripe\CMS\Controllers\ContentController;
class PageController extends ContentController
{
private static $allowed_actions = [
'rss'
];
2017-08-07 05:11:17 +02:00
2017-10-27 04:38:27 +02:00
public function init()
2017-08-07 05:11:17 +02:00
{
2017-10-27 04:38:27 +02:00
parent::init();
2019-03-23 14:22:37 +01:00
RSSFeed::linkToFeed($this->Link("rss"), "10 Most Recently Updated Pages");
2017-10-27 04:38:27 +02:00
}
public function rss()
{
$rss = new RSSFeed(
$this->LatestUpdates(),
$this->Link(),
"10 Most Recently Updated Pages",
"Shows a list of the 10 most recently updated pages."
);
2017-08-07 05:11:17 +02:00
2017-10-27 04:38:27 +02:00
return $rss->outputToBrowser();
}
public function LatestUpdates()
{
return Page::get()->sort("LastEdited", "DESC")->limit(10);
2017-08-07 05:11:17 +02:00
}
2017-10-27 04:38:27 +02:00
}
2017-08-03 05:35:09 +02:00
2017-08-03 02:51:32 +02:00
```
2011-02-07 07:48:44 +01:00
2012-09-18 09:42:21 +02:00
### Rendering DataObjects in a RSSFeed
2017-07-03 03:22:12 +02:00
DataObjects can be rendered in the feed as well, however, since they aren't explicitly [SiteTree ](api:SilverStripe\CMS\Model\SiteTree ) subclasses we
2014-09-28 05:59:48 +02:00
need to include a function `AbsoluteLink` to allow the RSS feed to link through to the item.
2012-09-18 09:42:21 +02:00
2019-11-18 05:58:33 +01:00
[info]
2014-09-28 05:59:48 +02:00
If the items are all displayed on a single page you may simply hard code the link to point to a particular page.
2019-11-18 05:58:33 +01:00
[/info]
2012-09-18 09:42:21 +02:00
2014-09-28 05:59:48 +02:00
Take an example, we want to create an RSS feed of all the `Players` objects in our site. We make sure the `AbsoluteLink`
method is defined and returns a string to the full website URL.
2012-09-18 09:42:21 +02:00
2017-08-03 02:51:32 +02:00
```php
2017-10-27 04:38:27 +02:00
use SilverStripe\Control\Controller;
use SilverStripe\Control\Director;
use SilverStripe\ORM\DataObject;
2017-08-07 05:11:17 +02:00
2017-10-27 04:38:27 +02:00
class Player extends DataObject
{
2017-08-07 05:11:17 +02:00
2017-10-27 04:38:27 +02:00
public function AbsoluteLink()
{
// assumes players can be accessed at yoursite.com/players/2
2017-08-07 05:11:17 +02:00
2017-10-27 04:38:27 +02:00
return Controller::join_links(
Director::absoluteBaseUrl(),
'players',
$this->ID
);
2017-08-07 05:11:17 +02:00
}
2017-10-27 04:38:27 +02:00
}
2017-08-03 02:51:32 +02:00
```
2012-09-18 09:42:21 +02:00
2014-09-28 05:59:48 +02:00
Then in our controller, we add a new action which returns a the XML list of `Players` .
2011-02-07 07:48:44 +01:00
2017-08-03 02:51:32 +02:00
```php
2017-10-27 04:38:27 +02:00
use SilverStripe\Control\RSS\RSSFeed;
use SilverStripe\CMS\Controllers\ContentController;
2017-08-05 00:45:24 +02:00
2017-10-27 04:38:27 +02:00
class PageController extends ContentController
{
2014-09-28 05:59:48 +02:00
2017-10-27 04:38:27 +02:00
private static $allowed_actions = [
'players'
];
2014-09-28 05:59:48 +02:00
2017-10-27 04:38:27 +02:00
public function init()
{
parent::init();
2014-09-28 05:59:48 +02:00
2017-10-27 04:38:27 +02:00
RSSFeed::linkToFeed($this->Link("players"), "Players");
}
2014-09-28 05:59:48 +02:00
2017-10-27 04:38:27 +02:00
public function players()
{
$rss = new RSSFeed(
Player::get(),
$this->Link("players"),
"Players"
);
2014-09-28 05:59:48 +02:00
2017-10-27 04:38:27 +02:00
return $rss->outputToBrowser();
2017-08-07 05:11:17 +02:00
}
2017-10-27 04:38:27 +02:00
}
2017-08-03 02:51:32 +02:00
```
2012-09-18 09:42:21 +02:00
### Customizing the RSS Feed template
2011-02-07 07:48:44 +01:00
2017-10-06 17:00:32 +02:00
The default template used for XML view is `vendor/silverstripe/framework/templates/RSSFeed.ss` . This template displays titles and links to
2016-03-30 02:17:28 +02:00
the object. To customise the XML produced use `setTemplate` .
2014-09-28 05:59:48 +02:00
Say from that last example we want to include the Players Team in the XML feed we might create the following XML file.
2018-06-25 00:39:53 +02:00
**app/templates/PlayersRss.ss**
2014-09-28 05:59:48 +02:00
2017-08-03 02:51:32 +02:00
```xml
2017-10-27 04:38:27 +02:00
<?xml version="1.0"?>
< rss version = "2.0" xmlns:dc = "http://purl.org/dc/elements/1.1/" xmlns:atom = "http://www.w3.org/2005/Atom" >
< channel >
< title > $Title< / title >
< link > $Link< / link >
< atom:link href = "$Link" rel = "self" type = "application/rss+xml" / >
< description > $Description.XML< / description >
< % loop $Entries %>
< item >
< title > $Title.XML< / title >
< team > $Team.Title< / team >
< / item >
< % end_loop %>
< / channel >
< / rss >
2017-08-03 02:51:32 +02:00
```
2012-09-18 09:42:21 +02:00
2014-09-28 05:59:48 +02:00
`setTemplate` can then be used to tell RSSFeed to use that new template.
2018-06-25 00:39:53 +02:00
**app/code/Page.php**
2012-09-18 09:42:21 +02:00
2017-08-03 02:51:32 +02:00
```php
2017-10-27 04:38:27 +02:00
use SilverStripe\Control\RSS\RSSFeed;
public function players()
{
$rss = new RSSFeed(
Player::get(),
$this->Link("players"),
"Players"
);
2017-08-07 05:11:17 +02:00
2017-10-27 04:38:27 +02:00
$rss->setTemplate('PlayersRss');
return $rss->outputToBrowser();
}
2017-08-03 02:51:32 +02:00
```
2011-02-07 07:48:44 +01:00
2019-11-18 05:58:33 +01:00
[warning]
2014-09-28 05:59:48 +02:00
As we've added a new template (PlayersRss.ss) make sure you clear your SilverStripe cache.
2019-11-18 05:58:33 +01:00
[/warning]
2011-02-07 07:48:44 +01:00
## API Documentation
2012-09-18 09:42:21 +02:00
2017-07-03 03:22:12 +02:00
* [RSSFeed ](api:SilverStripe\Control\RSS\RSSFeed )