Creating a custom View Helper in the Zend Framework

Zend Framework ships with several View Helpers, such as the url View Helper. Anywhere in a view script one can call $this->url() to generate a url to a certain page. For example:

$this->url(array(
  'module' => 'moduleName',
  'action' => 'actionName',
  'additionalParam' =>'value'))

I wanted to create my own helper, for generating paths to thumbnails, and it turned out to be really simple. Since I wanted to be able to access the helper from any module, I created the view helper class in /library/My/View/Helper/Thumbnail.php. The definition of the class looks like this:

class My_View_Helper_Thumbnail extends Zend_View_Helper_Abstract
{
  public function thumbnail($id, $type, $width, $height)
  {
    $thumbModel = new My_Model_Thumbnails();
    return $thumbModel
      ->getThumbnail($id, $width, $height);
  }
}

The getThumbnail() function will check if there exists a thumbnail (of size $width x $height) for the image with id $id, if one exists the path to it is returned. Otherwise the thumbnail will be generated on the fly.
The last piece of magic before it is possible to use our new view helper is to add one line to our bootstrap.php. After Zend MVC has been started, the path to our new view helper must be added to the layout in orders for view to be able to find it.

$layout = Zend_Layout::startMvc(APPLICATION_PATH . '/layouts/scripts');
$layout->getView()->addHelperPath('My/View/Helper', 'My_View_Helper');

When this is done it is possible to call $this->thumbnail() in all view scripts in the project.

Peter

4 thoughts on “Creating a custom View Helper in the Zend Framework

  1. I was writing a view helper function to do exactly the same and came across your example. Presumably My_Model_Thumbnails will have to query the database to determine the location of original image file referenced by $id; but then by getting to this view helper, you’ve probably already queried the database asking about this image.

    So… this does seem a bit inefficient. Would an alternative be to have an image object which is populated once from the database, and then stored within the view: $this->view->image = $image within the controller. Then in the view you could call the view helper with ($image, $width, $height) instead, and it would need no further db calls.

    But then… I’m thinking: why bother with the helper function – within the view you can just do: $this->image.Thumbnail($width, $height), where Thumbnail is a method of the image object.

    I’m wondering what the best approach is, so would be interested in your comments on the above.

  2. Dan,

    In my project I do not store any information about the image or thumbnail path in the database. Both the originals and the thumbnails are stored on disk (not in database). The location of the image and thumbnail is determined from the type and the id of the image (a thumbnail is created on the fly if you request a size that does not exist). Therefore this is not an issue for me.

    In the scenario you described, where you need to query the database to get the image path, it would make sense to store the image object somewhere – to avoid further database calls if you need the image again. If you have modeled your image class to have a Thumbnai() function, the helper method is not really needed.

    -Peter

Leave a Reply

Your email address will not be published. Required fields are marked *