The Blogs Page: The feed/digest action handler
Friday, April 9, 2010

The feed controller digest action handler is used to render a list of the five most recent blog posts on the specified blog, as exposed by the blog’s RSS feed.

The feed controller is implemented as follows:

class FeedController extends Zend_Controller_Action
{
  public function digestAction()
  {
    try
    {
      $this->_helper->layout->disableLayout();
 
      $id = $this->getRequest()->getParam('id');
 
      if (isset($id))
      {
        $dbBlog = new Default_Model_Db_Blog();
        $blog = $dbBlog->fetchBlog($id);
 
        if (isset($blog))
        {
          $this->view->blog = $blog;
        }
        else
        {
          $this->view->errorMessage = 'Blog ID not found in database';
        }
      }
      else
      {
        $this->view->errorMessage = 'Blog ID not specified';
      }
    }
    catch (Exception $e)
    {
      $this->view->errorMessage = $e->getMessage();
    }
  }
}

The function implementation is wrapped in an exception handler, mainly to catch any database errors. This is the basic use case:

  1. As it is not going to render an entire page, but rather respond to an AJAX request, it calls the layout helper to disable layout rendering.
  2. It checks the value of the id parameter, which should have been passed via the URL.
  3. If the id is set, it retrieves the database record in the blog table for that id.
  4. If a blog record is returned, it passes that to the view script.

There are three deviations from this:

  1. If the id is not set, it reports an error (‘Blog ID not specified’).
  2. If no database record is returned for the specified id, it reports an error (‘Blog ID not found in database’).
  3. If an exception is throw, it reports an error; the exact message will depend on the exception.

The implementation of Default_Model_Db_Blog has been covered in previous posts (see the post on the data classes and the post on the mapper classes).

How the blog record and any error messages are handled in the associated view script is the subject of the next post.

Posted by James at 7:18 am   0 comments

Leave a Reply