Category: Views

Q: RSS feed view in CakePHP 3

I want to create a view of news for RSS in CakePHP. Now I have all posts set to /news/rss.ctp

public function rss() {

    //get recent news

    $this->set('rss', $rss);

What to do next?


expected output..

<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
 <title>RSS Title</title>
 <description>This is an example of an RSS feed</description>
 <lastBuildDate>Mon, 06 Sep 2010 00:01:00 +0000 </lastBuildDate>
 <pubDate>Sun, 06 Sep 2009 16:20:00 +0000</pubDate>

  <title>Example entry</title>
  <description>Here is some text containing an interesting description.</description>
  <guid isPermaLink="true">7bd204c6-1655-4c27-aeee-53f933c5395f</guid>
  <pubDate>Sun, 06 Sep 2009 16:20:00 +0000</pubDate>

2 Answers

Version: 3.3



Creating Useful Routes

// In config/routes.php...

Router::scope('/news/rss', function ($routes) {

From the official documentation, in your news controller:

public function initialize()

// Modify the Posts Controller action that corresponds to
// the action which deliver the rss feed, which is the
// Index action in our example.
public function rss()
    if ($this->RequestHandler->isRss() ) {
        $articles = $this->Articles
            ->order(['created' => 'desc']);
    } else {
        // this is not an Rss request, so deliver
        // data used by website's interface.
        $this->paginate = [
            'order' => ['created' => 'desc'],
            'limit' => 10
        $this->set('articles', $this->paginate($this->Articles));
        $this->set('_serialize', ['articles']);

Ref: Creating an RSS Feed with the RssHelper