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?


updated:

expected output..

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

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

</channel>
</rss>
rss
2 Answers

Version: 3.3

user
Doug

3

Creating Useful Routes

// In config/routes.php...

Router::scope('/news/rss', function ($routes) {
    $routes->extensions(['rss']);
    $routes->resources('News');
});

From the official documentation, in your news controller:

public function initialize()
{
    parent::initialize();
    $this->loadComponent('RequestHandler');
}

// Modify the Posts Controller action that corresponds to
// the action which deliver the rss feed, which is the
// Index action in our example.
//'/news/rss'
public function rss()
{
    if ($this->RequestHandler->isRss() ) {
        $articles = $this->Articles
            ->find()
            ->limit(20)
            ->order(['created' => 'desc']);
        $this->set(compact('articles'));
    } 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