Kristian Lunde

The View Helper pattern

with 4 comments

Developing MVC (Model View Controller pattern) applications in PHP or any other language often require a lot from the view tier. The view needs to process data received from the model tier and form it into presentable data, it also has to manage user input and form that into data understandable for the model tier.
This might not be a big issue while working with small application, but when it comes to midscale and large applications the view helper pattern can be of great help. The view helper pattern is one of the J2EE core patterns and the documentation can be found on:

What does the helper pattern do?
First of all the helper pattern adds an extra tier to the system, this tier can be seen as a mid tier which has some understanding of the logic of the system, it knows a little bit about the view and a little bit about the model. Another cool thing is that the view helper pattern makes your code more reusable. When moving complex structures from the view and into a view helper it can with ease be used by other views.


You are writing a web application where the user writes a review of some product, the application should do auto saving of the user input every 20 second using Ajax functionality. The application should of course also save the user input then the user submits the data. The ajax request and the user submit does almost the same thing but the when the user submits the data the view should also store a rating of the product. This require the ajax request and the user submit to be two different views, or one complex view.

In an ordinary MVC system you would have to implement two views with very much of the similar behavior or one complex view. Using the view helper pattern you extract the storage of the user review in a helper which can be reused by both the ajax request view and the user submit view.

I have written a very simple implementation of the example in PHP. The implementation is not complete at all, but it is meant as a proof of concept that the reusability of code in your application can increase using the view helper pattern.

  1. <?php
  3. class ProductReviewHelper
  4. {
  5.  public function __construct(){}
  7.  /**
  8.    * save the review
  9.   **/
  10.  public function save($user_input)
  11.  {
  12.   //validate input
  13.   $input = $this->validate($user_input);
  15.   //saves the review and return the result of the save
  16.   return $review_manager->save($input['product_id'], $input['review']);
  17.  }
  19.  /**
  20.   * validate the input
  21.   **/
  22.  public function validate($user_input)
  23.  {
  24.   $filter_args = array('product_id' => FILTER_VALIDATE_INT,
  25.          'review'   => FILTER_SANITIZE_STRING);
  26.   $input = filter_var_array($user_input, $filter_args);
  28.   //do validation
  29.   return $input;
  30.  }
  31. }
  1. <?php
  3. //Ajax view
  4. $review_helper = new ProductReviewHelper();
  5. $result = $review_helper->save($_POST);
  6. echo $result;
  7. exit();
  8. ?>
  1. <?php
  3. //User submit view
  4. $review_helper = new ProductReviewHelper();
  5. $review_result = $review_helper->save($_POST);
  7. $rating_helper = new ProductRatingHelper();
  8. $rating_result = $rating_helper->save($_POST);
  10. //manage the result from the helpers
  11. ?>

I am sure that the view helper pattern has helped me to write better and more organized code, which is easily understandable and very reusable.

If you do not use the view helper pattern, and still have solved the problem with reusability of code in the view tier please feel free to leave me a comment describing your solution.

Be Sociable, Share!

Written by Kristian Lunde

July 20th, 2008 at 11:11 pm

4 Responses to 'The View Helper pattern'

Subscribe to comments with RSS or TrackBack to 'The View Helper pattern'.

  1. Hi,

    I used Zend Framework about 2 years back for awhile on a project I was porting. From what I can gather this is more of a model type object or a model helper, or even a helper for the controller. I thought a ViewHelper wraps up consistent View logic for reuse, relevant only to a View, for example getting data and generating a table instead of putting a loop directly in your View.

    What do you think?

    P.S. good article other than my points ;)

    JP Sheridan

    3 Aug 12 at 11:51:55

  2. Hi JP,

    I agree, I use Zend Framework a lot and I use view helpers the way you describe it. I guess we can call what I’ve blogged about in this post more of a controller helper than a view helper in my opinion, agree?


    Kristian Lunde

    11 Aug 12 at 20:15:18

  3. Hi Kristian,

    I agree. Either way, like I said, great article. I was away from coding for quite awhile and recently gained employment so I’m happy to find articles related to what I’m working on, like yours ;)


    JP Sheridan

    28 Aug 12 at 15:55:13

  4. Thanks JP, I am glad you enjoyed it :)

    Kristian Lunde

    30 Aug 12 at 06:26:11

Leave a Reply

Get Adobe Flash player