Web Frameworks - Workbook - Week 06

From mi-linux
Revision as of 16:01, 19 January 2010 by In9352 (talk | contribs)
Jump to navigationJump to search

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

Consuming an existing web feed

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

% zf.sh create action feed guestbook

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://newsrss.bbc.co.uk/rss/newsonline_uk_edition/front_page/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;
    }

Next, we need to edit our view and add some code to loop through and display feed data passed 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>

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.