source code bean

14 Feb, 2009

Creating a custom View Helper in the Zend Framework

Posted by: Peter In: PHP| 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:

  1. $this->url(array(
  2.   ‘module’ => ‘moduleName’,
  3.   ‘action’ => ‘actionName’,
  4.   ‘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:

  1. class My_View_Helper_Thumbnail extends Zend_View_Helper_Abstract
  2. {
  3.   public function thumbnail($id, $type, $width, $height)
  4.   {
  5.     $thumbModel = new My_Model_Thumbnails();
  6.     return $thumbModel
  7.       ->getThumbnail($id, $width, $height);
  8.   }
  9. }

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.

  1. $layout = Zend_Layout::startMvc(APPLICATION_PATH . ‘/layouts/scripts’);
  2. $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.

3 Responses to "Creating a custom View Helper in the Zend Framework"

1 | Dan

April 19th, 2009 at 5:09 pm

Avatar

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 | Peter

May 13th, 2009 at 1:52 pm

Avatar

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

3 | Creating a JSON-RPC service using Zend Json Server

January 7th, 2010 at 8:13 am

Avatar

[...] Creating a custom View Helper in the Zend Framework [...]

Comment Form

Categories

Adwords

Twitter Updates


    • Petan: I got in bootstrap this (insted of $frontController->getRouter()): $config = new Zend_Config_Ini(APPLICATION_PATH . '/configs/routes.ini', 'rout
    • oanh tong ngoc: :) It's usefull but could U give everyone's an example with a project source code. Thanks
    • Peter: Hi Sohaib, It seems like the rewrite module isn't loaded by IIS. Have you uploaded the UrlRewriter dlls and made the changes to web.config on the ser

    About

    Welcome to source code bean! You will find information on tips and tricks on programming languages, server side stuff, and anything that causes troubles to web development.