Accessing and using Zend View Helpers from a common directory

In this tutorial I am going to describe a typical scenario where we use Zend View Helpers to do some common view task throughout the application. Moreover we want this helper to be located in some common place/directory from where it is accessible to any module in an application.

Typically, for each module we do replicate the directory structure of controllers, model and views and so does the helpers directory inside the view directory. Zend automatically tries to search the helpers at the module level helper directory and other helper paths that might have been registered with the View object in the action code of a controller. Because of this behavior of Zend if a helper is needed globally throughout the application; for example a helper is called from a Layout file shared amongst different module, then it requires that either the helper be duplicated in each module directory or be registered with the View object in all the actions of the application where its required. Both these methods are not recommended as they duplicate the code and are difficult to maintain.

Ideally, there should be a directory where all such globally called helpers would reside. Lets call them Global View Helpers or just Global Helpers. Also, these global helpers are registered with View object (add helper path) from a single location such that they are automatically available to all modules and can be called from anywhere inside the application.

To implement this lets create a GlobalViewHelpers directory inside the top application directory. So that it would look like,

//typical Zend directory structure
 
+Application
+-->Controllers
+-->Model
+-->View
+-->GlobalViewHelpers //our global helpers directory..
+--+-->SiteUrl.php
+public

Here I have created one global view helper SiteUrl.php and placed in the GlobalViewHelpers Directory. the Helper code looks some thing like below.

<?php
 
//SiteUrl.php 
 
//Custom View Helper Class for my project
class Zend_View_Helper_SiteUrl extends Zend_View_Helper_Abstract
{
private $siteUrl = "";
 
//The class must have a public method that matches the helper name
 public function siteUrl()
 {
   if($this->siteUrl == "")
   {		
     $config = Zend_Registry::getInstance()->get('config');
     $this->siteUrl = $config->site_url;
    }
   return $this->siteUrl;
 }
 
}
 
 
?>

Once you have the directory structure and helper created, all you need to do is to add the GlobalViewHelpers directory path to helper paths of the view object. Put the below code inside your Bootstrap file (in my case its my index.php file) so that on every execution the path gets registered to the View Object and Zend can find the appropriate global helper called anywhere from the application

//Bootstrapping file..
 
//Initialize and/or retrieve a ViewRenderer object on demand via the helper broker
$viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
$viewRenderer->initView();
 
//add the global helper directory path
$viewRenderer->view->addHelperPath('/your/path/to/GlobalViewHelpers');

After this, inside my Layout file (.phtml) I can now call this global helper like below from any module without any need to duplicate the helper class module wise are adding helper path in each action.

<?php echo $this->siteUrl(); ?>

I hope this tutorial is useful to you and helps you solve your problem in such scenario and write better code. Your comments and feedback are welcome.