Welcome to Abdul Malik Ikhsan's Blog

Zend Framework 2 : ViewFeedStrategy : Generating Feeds

Posted in Tutorial PHP, Zend Framework 2 by samsonasik on September 18, 2012

Beside DefaultRenderingStrategy, ViewPhpRendererStrategy, and ViewJsonStrategy, Zend Framework 2 has ViewFeedStrategy that can be used in rendering generated feeds. It use FeedModel to utilize the strategy.

The enable the ViewFeedStrategy, we simply configure it in the module’s config.php :

return aray(
   'controllers'=> array( /* common code */ ),
   'router' => array( /* common code */ ),

   'view_manager' => array(
        'strategies' => array(

let’s test it :

namespace ZendSkeletonModule\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\Feed\Writer\Feed;
use Zend\View\Model\FeedModel;

class SkeletonController extends AbstractActionController
    public function feedAction()
        $feed = new Feed();
        $feed->setTitle('Feed Example');
        $feed->setFeedLink('http://ourdomain.com/rss', 'atom');
            'name'  => 'admin',
            'email' => 'contact@ourdomain.com',
            'uri'   => 'http://www.ourdomain.com',
        $feed->setDescription('Description of this feed');

        //fake data...
        $data = array(
            0 => array('title' => 'my 1st post', 'link' => 'http://ourdomain.com/1stpost',
                       'content'=> 'summary of 1st post',
                       'date_created' => '2012-01-01 00:00:00',
                       'date_created' => '2012-01-01 00:00:00'
            1 => array('title' => 'my 2nd post', 'link' => 'http://ourdomain.com/2ndpost',
                       'content'=> 'summary of 2nd post',
                       'date_created' => '2012-02-01 00:00:00',
                       'date_created' => '2012-02-01 00:00:00'),
            2 => array('title' => 'my 3rd post', 'link' => 'http://ourdomain.com/3rdpost',
                       'content'=>'summary of 3rd post',
                       'date_created' => '2012-03-01 00:00:00',
                       'date_created' => '2012-03-01 00:00:00'),

        foreach($data as $row)
            //create entry...
            $entry = $feed->createEntry();




        $feedmodel = new FeedModel();

        return $feedmodel;

Generated feeds :

Done !

Reference :
1. http://akrabat.com/zend-framework-2/returning-json-from-a-zf2-controller-action/

10 Responses

Subscribe to comments with RSS.

  1. Zend Framework Johny said, on September 19, 2012 at 3:56 am

    Looks nice and tested,
    maybe it would even be possible to have some kind of mapper for a db result set which would make it easier,
    but for a simple application this should be enough.

  2. Quamar Brooks said, on October 26, 2012 at 12:10 pm

    Zend Framework 2 has View FeedStrategy that can be used in making produced for .It use Feed Model to implement the technique

  3. Will Sewell said, on February 10, 2013 at 4:47 pm

    I’ve been struggling to get this working for a while now (being a ZF2 newbie). I’m very greatful to see this post because it shows me I’m on the right track. I’ve implemented it in much the same way that you have, but I get the error:

    PHP Fatal error: Uncaught exception ‘Zend\\View\\Exception\\RuntimeException’ with message ‘Zend\\View\\Renderer\\PhpRenderer::render: Unable to render template “blog/blog/atom”; resolver could not resolve to a file’

    Do you know why ZF2 is attempting to use PhpRenderer rather than FeedRenderer?

    • samsonasik said, on February 10, 2013 at 5:57 pm

      It should be work. try to die it. by using FeedModel, you not pass it to the view.

      • Will Sewell said, on February 17, 2013 at 5:53 pm

        Thanks. It turned out to be a stupid typo. I spelled “strategies” as “stratgies”. I generally rely on my IDE to pick out badly named symbol references, which is why I hate referencing symbols as strings.

  4. jhekasoftjhekasoft said, on June 30, 2013 at 7:47 pm

    This string isn’t need: $feed->export(‘rss’);

  5. lmcosorio said, on May 25, 2014 at 11:09 pm

    Is there anything you don’t know about Zend Framework? For everything I need and search online, here’s your website to the rescue!

    Thank you Samsonasik

    Luis Osorio

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: