Web Frameworks - Workbook - Week 06

From mi-linux
Jump to navigationJump to search

Main Page >> Web Frameworks >> Workbook >> Workshop - week 06

Web feeds

Today we'll learn how to consuming an existing web feed.

Step 1 - create a new action and view

First let's create a new "feed" action in our existing "guestbook" controller:

% zf.sh create action feed guestbook

As usual don't forget to chmod the newly created files.

Step 2 - edit new action

Next, let's add some code in our new action.

Reading an RSS feed is as simple as instantiating a Zend_Feed_Rss object with the URL of the feed.

We then pass the feed data to the view.

// application/controllers/GuestbookController.php

    public function feedAction()
    {
      // Read remote feed
      try
      {
          $rss = new Zend_Feed_Rss('http://news.sky.com/sky-news/rss/home/rss.xml');
      } 
      catch (Zend_Feed_Exception $e)
      {
          // feed import failed
          echo "Exception caught importing feed: {$e->getMessage()}\n";
          exit;
      }

      // Pass feed data to view
      $this->view->entries = $rss;
    }

Step 3 - edit new view

Next, we need to edit our view and add some code to loop through and display the feed data passed in by the controller:


<!-- application/views/scripts/guestbook/feed.phtml -->

<!-- Display feed title -->
<h1><?=$this->entries->title()?></h1>
<dl>
    <!-- Loop through the entries provided by the controller -->
    <? foreach ($this->entries as $entry): ?>
    <dt><?= $this->escape($entry->title()) ?></dt>
    <dd><?= $this->escape($entry->description()) ?></dd>
    <? endforeach ?>
</dl>

Step 4 - add link to feed

Finally, let's add a link to our feed in our navigation menu. To do this, add the following code in our existing layout:

        - <a href="<?php echo $this->url(
            array('controller'=>'guestbook', 'action'=>'feed'), 
            'default', 
            true) ?>">Feed</a>

Our whole layout should now look like this:

<?php 
// application/layouts/scripts/layout.phtml

echo $this->doctype() ?>
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head>  
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
  <title>Zend Framework Quickstart Application</title>
  <?php echo $this->headLink()->appendStylesheet('/css/global.css') ?>
</head> 
<body>
<div id="header" style="background-color: #EEEEEE; height: 30px;">
    <div id="header-logo" style="float: left">
        <b>ZF Quickstart Application</b>
    </div>
    <div id="header-navigation" style="float: right">
        <a href="<?php echo $this->url(
            array('controller'=>'guestbook'), 
            'default', 
            true) ?>">Guestbook</a>
            
        - <a href="<?php echo $this->url(
            array('controller'=>'guestbook', 'action'=>'feed'), 
            'default', 
            true) ?>">Feed</a>
            
    </div>
</div>

<?php echo $this->layout()->content ?>

</body>
</html>

Bg-checkpoint.png Checkpoint

This is what you should see on your screen when clicking on your new "Feed" link in your navigation bar:

Zend 05.jpg

Note: For simplicity and clarity we are importing the feed in the controller, but a feed is an XML file, and as such could (should?) be considered as a data source. In line with MVC principles it would therefore be better practice to move the feed import code to a "model" file.

More on feeds

For more information on web feeds, see Zend documentation : http://framework.zend.com/manual/en/zend.feed.html