2014-09-28 05:59:48 +02:00
|
|
|
title: RSS Feed
|
|
|
|
summary: Output records from your database as an RSS Feed.
|
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
|
|
|
|
2016-02-17 22:21:59 +01:00
|
|
|
Generating RSS / Atom-feeds is a matter of rendering a [api:SS_List] instance through the [api:RSSFeed] class.
|
2011-02-07 07:48:44 +01:00
|
|
|
|
2016-02-17 22:21:59 +01:00
|
|
|
The [api: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 [api: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
|
2016-02-17 22:21:59 +01:00
|
|
|
advisable to just create feeds from subclasses of [api:SiteTree].
|
2012-09-18 09:42:21 +02:00
|
|
|
|
2014-09-28 05:59:48 +02:00
|
|
|
<div class="warning" markdown="1">
|
2016-02-17 22:21:59 +01:00
|
|
|
If you wish to generate an RSS feed that contains a [api:DataObject], ensure you define a `AbsoluteLink` method on
|
2014-09-28 05:59:48 +02:00
|
|
|
the object.
|
|
|
|
</div>
|
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
|
|
|
|
|
|
|
:::php
|
|
|
|
$feed = new RSSFeed(
|
2014-09-21 02:07:58 +02:00
|
|
|
$list,
|
|
|
|
$link,
|
|
|
|
$title,
|
|
|
|
$description,
|
|
|
|
$titleField,
|
|
|
|
$descriptionField,
|
2014-01-26 00:06:07 +01:00
|
|
|
$authorField,
|
|
|
|
$lastModifiedTime,
|
|
|
|
$etag
|
2012-09-18 09:42:21 +02:00
|
|
|
);
|
|
|
|
|
2014-09-28 05:59:48 +02:00
|
|
|
$feed->outputToBrowser();
|
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.
|
|
|
|
|
2011-02-07 07:48:44 +01:00
|
|
|
:::php
|
2014-09-28 05:59:48 +02:00
|
|
|
RSSFeed::linkToFeed($link, $title);
|
|
|
|
|
|
|
|
## 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
|
|
|
|
2016-02-17 22:21:59 +01:00
|
|
|
You can use [api: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.
|
|
|
|
|
|
|
|
**mysite/code/Page.php**
|
2011-02-07 07:48:44 +01:00
|
|
|
|
|
|
|
:::php
|
|
|
|
<?php
|
2014-09-28 05:59:48 +02:00
|
|
|
|
|
|
|
..
|
|
|
|
|
2011-02-07 07:48:44 +01:00
|
|
|
class Page_Controller extends ContentController {
|
2014-09-21 02:07:58 +02:00
|
|
|
|
2014-09-28 05:59:48 +02:00
|
|
|
private static $allowed_actions = array(
|
|
|
|
'rss'
|
|
|
|
);
|
2014-09-21 02:07:58 +02:00
|
|
|
|
2012-01-30 23:13:42 +01:00
|
|
|
public function init() {
|
2011-02-07 07:48:44 +01:00
|
|
|
parent::init();
|
2014-09-28 05:59:48 +02:00
|
|
|
|
2014-09-21 02:07:58 +02:00
|
|
|
RSSFeed::linkToFeed($this->Link() . "rss", "10 Most Recently Updated Pages");
|
2011-02-07 07:48:44 +01:00
|
|
|
}
|
2014-09-21 02:07:58 +02:00
|
|
|
|
2012-01-30 23:13:42 +01:00
|
|
|
public function rss() {
|
2014-09-28 05:59:48 +02:00
|
|
|
$rss = new RSSFeed(
|
|
|
|
$this->LatestUpdates(),
|
|
|
|
$this->Link(),
|
|
|
|
"10 Most Recently Updated Pages",
|
|
|
|
"Shows a list of the 10 most recently updated pages."
|
|
|
|
);
|
|
|
|
|
2012-09-18 09:42:21 +02:00
|
|
|
return $rss->outputToBrowser();
|
2011-02-07 07:48:44 +01:00
|
|
|
}
|
2014-09-21 02:07:58 +02:00
|
|
|
|
2012-01-30 23:13:42 +01:00
|
|
|
public function LatestUpdates() {
|
2012-06-23 00:32:43 +02:00
|
|
|
return Page::get()->sort("LastEdited", "DESC")->limit(10);
|
2014-09-21 02:07:58 +02:00
|
|
|
}
|
2011-02-07 07:48:44 +01:00
|
|
|
}
|
|
|
|
|
2012-09-18 09:42:21 +02:00
|
|
|
### Rendering DataObjects in a RSSFeed
|
|
|
|
|
2016-02-17 22:21:59 +01:00
|
|
|
DataObjects can be rendered in the feed as well, however, since they aren't explicitly [api: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
|
|
|
|
2014-09-28 05:59:48 +02:00
|
|
|
<div class="info">
|
|
|
|
If the items are all displayed on a single page you may simply hard code the link to point to a particular page.
|
|
|
|
</div>
|
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
|
|
|
|
|
|
|
:::php
|
2014-09-21 02:07:58 +02:00
|
|
|
<?php
|
2014-09-28 05:59:48 +02:00
|
|
|
|
|
|
|
class Player extends DataObject {
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function AbsoluteLink() {
|
2014-09-28 05:59:48 +02:00
|
|
|
// assumes players can be accessed at yoursite.com/players/2
|
|
|
|
|
|
|
|
return Controller::join_links(
|
|
|
|
Director::absoluteBaseUrl(),
|
|
|
|
'players',
|
|
|
|
$this->ID
|
|
|
|
);
|
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
|
|
|
|
2012-09-18 09:42:21 +02:00
|
|
|
:::php
|
2014-09-28 05:59:48 +02:00
|
|
|
<?php
|
|
|
|
|
2012-09-18 09:42:21 +02:00
|
|
|
class Page_Controller extends ContentController {
|
2014-09-28 05:59:48 +02:00
|
|
|
|
|
|
|
private static $allowed_actions = array(
|
|
|
|
'players'
|
|
|
|
);
|
|
|
|
|
2012-09-18 09:42:21 +02:00
|
|
|
public function init() {
|
|
|
|
parent::init();
|
2014-09-28 05:59:48 +02:00
|
|
|
|
|
|
|
RSSFeed::linkToFeed($this->Link("players"), "Players");
|
2014-09-21 02:07:58 +02:00
|
|
|
}
|
2014-09-28 05:59:48 +02:00
|
|
|
|
|
|
|
public function players() {
|
2012-09-18 09:42:21 +02:00
|
|
|
$rss = new RSSFeed(
|
2014-09-28 05:59:48 +02:00
|
|
|
Player::get(),
|
|
|
|
$this->Link("players"),
|
|
|
|
"Players"
|
2012-09-18 09:42:21 +02:00
|
|
|
);
|
2014-09-28 05:59:48 +02:00
|
|
|
|
2012-09-18 09:42:21 +02:00
|
|
|
return $rss->outputToBrowser();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
### Customizing the RSS Feed template
|
2011-02-07 07:48:44 +01:00
|
|
|
|
2014-09-28 05:59:48 +02:00
|
|
|
The default template used for XML view is `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.
|
|
|
|
|
|
|
|
**mysite/templates/PlayersRss.ss**
|
|
|
|
|
|
|
|
:::xml
|
|
|
|
<?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>
|
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.
|
|
|
|
|
|
|
|
**mysite/code/Page.php**
|
2012-09-18 09:42:21 +02:00
|
|
|
|
|
|
|
:::php
|
2014-09-28 05:59:48 +02:00
|
|
|
|
|
|
|
public function players() {
|
2012-09-18 09:42:21 +02:00
|
|
|
$rss = new RSSFeed(
|
2014-09-28 05:59:48 +02:00
|
|
|
Player::get(),
|
|
|
|
$this->Link("players"),
|
|
|
|
"Players"
|
2012-09-18 09:42:21 +02:00
|
|
|
);
|
2014-09-28 05:59:48 +02:00
|
|
|
|
|
|
|
$rss->setTemplate('PlayersRss');
|
|
|
|
|
2012-09-18 09:42:21 +02:00
|
|
|
return $rss->outputToBrowser();
|
|
|
|
}
|
2011-02-07 07:48:44 +01:00
|
|
|
|
2014-09-28 05:59:48 +02:00
|
|
|
<div class="warning">
|
|
|
|
As we've added a new template (PlayersRss.ss) make sure you clear your SilverStripe cache.
|
|
|
|
</div>
|
2011-02-07 07:48:44 +01:00
|
|
|
|
|
|
|
|
|
|
|
## API Documentation
|
2012-09-18 09:42:21 +02:00
|
|
|
|
2016-02-17 22:21:59 +01:00
|
|
|
* [api:RSSFeed]
|