Skip to content
This repository has been archived by the owner on Jan 16, 2019. It is now read-only.

Docs RSS

Frank Kleine edited this page Apr 7, 2012 · 1 revision

Table of Contents

Create and display RSS feeds

Create RSS feeds

Using the generator

To create an RSS feed the net::stubbles::xml::rss::stubRSSFeedGenerator can be used. Here is an example how it can be used to create an RSS feed:

$rssFeedGenerator = new stubRSSFeedGenerator('My RSS feed', 'http://example.com/', 'My personal RSS feed created with Stubbles.');
foreach ($newsArticles as $newsArticle) {
    $rssFeedGenerator->addItem($newsArticle->getTitle(), 'http://example.com/article/' . $newsArticle->getId(), $newsArticle->getTeaser());
}
echo $rssFeedGenerator->serialize(stubXMLStreamWriterFactory::createAsAvailable())->asXML();

This is the most basic usage, but of course you want to set more properties like author, publishing date of the entry, ans so on. It is very easy to set more attributes on created items as the item instance is returned from the addItem() method:

$rssFeedGenerator->setLanguage('en_EN')
                 ->setCopyright('(c) 2008 Stubbles Development Team');
foreach ($newsArticles as $newsArticle) {
    $rssFeedGenerator->addItem($newsArticle->getTitle(), 'http://example.com/article/' . $newsArticle->getId(), $newsArticle->getTeaser())
                     ->byAuthor($newsArticle->getAuthor())
                     ->inCategory($newsArticle->getCategory())
                     ->withGuid($newsArticle->getId())
                     ->andGuidIsPermaLink(true)
                     ->publishedOn($newsArticle->getDate('Y-m-D'))
                     ->addCommentsAt('http://example.com/article/' . $newsArticle->getId() . '/comments')
                     ->withContent($newsArticle->getContent());
}

For a full list of available methods please consult the API docs.

Creating an RSS feed from entities

In most cases you already have other classes representing the content to be delivered as RSS, e.g. you may already have a NewsArticle class with informations about the author, the headline, and much more. Now it might be a little inconvenient to write such a large code block as above just to transform the article into an entry on the RSS feed, but Stubbles contains a solution that makes these steps very easy: you just have to annotate the NewsArticle class with the @RSSFeedItem annotation.

/**
*Entity representing a news article
 *
*@RSSFeedItem
*/
class NewsArticle
{
    // code of the class
}

Now you can use the rss feed generator as follows:

$rssFeedGenerator = new stubRSSFeedGenerator('My RSS feed', 'http://example.com/', 'My personal RSS feed created with Stubbles.');
foreach ($newsArticles as $newsArticle) {
    $rssFeedGenerator->addEntity($newsArticle);
}
echo $rssFeedGenerator->serialize(stubXMLStreamWriterFactory::createAsAvailable())->asXML();

Now each available information from the news article will be put into the rss feed without the need to write the whole block.

By default, a class annotated with @RSSFeedItem is expected to provide the following methods:

  • getTitle()*
  • getLink()*
  • getDescription()*
  • getAuthor()
  • getCategories()
  • getCommentsURL()
  • getEnclosures()
  • getGuid()
  • isPermaLink()
  • getPubDate()
  • getSources()
  • getContent()

Methods marked with * are mandatory (but may be optional if overrides are used, see below), all others are optional.

However, not in all cases the methods of your entity have such names. In this cases it is possible to configure the annotation, so that the alternate methods will be used instead:

/**
*Entity representing a news article
 *
*@RSSFeedItem(titleMethod='getHeadline',
*linkMethod='getArticleLink',
*descriptionMethod='getTeaser',
*)
*/
class NewsArticle
{
    // code of the class
}

The entity must at least provide methods for returning the title, the link and the description. Every other method is optional.

All possible parameters for the annotation:

  • titleMethod
  • linkMethod
  • descriptionMethod
  • authorMethod
  • categoriesMethod
  • commentsMethod
  • enclosuresMethod
  • guidMethod
  • isPermaLinkMethod
  • pubDateMethod
  • sourcesMethod
  • contentMethod

Sometimes it is not useful to have informations about the link available in the entity because the link does not belong to the core business of the entity. Here come overrides into play:

$rssFeedGenerator = new stubRSSFeedGenerator('My RSS feed', 'http://example.com/', 'My personal RSS feed created with Stubbles.');
foreach ($newsArticles as $newsArticle) {
    $rssFeedGenerator->addEntity($newsArticle, array('link' => 'http://example.com/article/' . $newsArticle->getId());
}
echo $rssFeedGenerator->serialize(stubXMLStreamWriterFactory::createAsAvailable())->asXML();


All overrides are supplied as second parameter. In the example above, the NewsArticle class now does not need to contain any informations about links, it does not even have to provide a method to return the link. Here is the list of keys to be used in the override array:

  • title
  • link
  • description
  • byAuthor
  • inCategories
  • addCommentsAt
  • deliveringEnclosures
  • withGuid
  • andGuidIsPermaLink
  • publishedOn
  • inspiredBySources
  • withContent

Display RSS feeds

Stubbles offers support for direct inclusion of RSS feeds into the front controller. The net::stubbles::xml::rss::stubRSSProcessor can be configured as a processor. The RSS feeds then need to be created within an implementation of net::stubbles::xml::rss::stubRSSFeed.

Configuring RSS feeds

RSS feeds to be delivered by the net::stubbles::xml::rss::stubRSSProcessor class can be configured in projects/$YOUR_PROJECT/config/rss-feeds.ini:

articles="my::own::package::ArticlesRssFeed"
comments="my::own::package::CommentsRssFeed"

The RSS processor listens for a request parameter named feed. The value of this parameter is evaluated. With the config above, if its value is comments, the rss processor would instantiate my::own::package::CommentsRssFeed then and call its create() method, as it is expected to implement the net::stubbles::xml::rss::stubRSSFeed interface. If the request parameter is not set or contains an unknown feed, the rss processor will just use the first configured feed, in this case the my::own::package::ArticlesRssFeed.

Clone this wiki locally