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.

  • John Johnson

    Thanks Penuel,

    Very helpful little tutorial

    Cheers, John

  • Penuel

    Hey John, Thanks for your comments!!

  • Jp

    Nice man, is what i was looking for

  • Penuel

    @Jp, Glad you it helped you

  • Otto Mora

    Exactly what I was looking for! Thanks a ton!

  • Pingback: Using Common View Helpers | ZFGoodies

  • miriam

    I want to override “formradio” helper. I want to use my helper from every form in my app. I’ve tried to locate my helper in many locations but- it was not recognized anywhere I put it..

    I’ve tried to add a line in the ini file, to call it strictly and to add the lines in my bootstarp file- but it doesn’t work at all! any advise for me??

    my bootstarp file code is the base one:
    bootstrap(‘view’);
    $view = $this->getResource(‘view’);
    //$view->addHelperPath(‘My/View/Helper/’, ‘My_Helper’);
    $view->doctype(‘XHTML1_STRICT’);
    }
    }

    thanks in advance!
    miriam

  • http://www.codetoon.com Penuel

    Miriam, I am not sure what you are trying to do in your code above. Which version of Zend are you using?

  • Sudhir

    Thanks Penuel. Helped me a lot, exactly what i was looking for. Thanks again….

  • Allan Chappell

    I’ve been messing around with Rob Allen’s Zend_Auth tutorial, and he doesn’t got into this detail about this subject. I’ve been trying for hours to figure out how to do/find exactly what you’ve got here so that I can implement his LoggedInAs view helper globally across all modules.

    Particularly the following helped


    //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');

    I shall share this answer with all who I’ve come across with the same delima.

    Thanks Again!

  • Vlado

    as you see it here:
    http://akrabat.com/zend-framework/zend-framework-view-helpers/

    you’d better add a config line like this:

    resources.view.helperPath.App_View_Helper_ = “App/View/Helper/”

    in your appilication.ini file

    Cheers,
    Vlado

  • Dhiren

    Thanks Penuel. This helped me a lot. As per article you asked us to put below code in index.php but i was getting Zend_Controller_Action_HelperBroker not found error. Then i added below in line in Bootstrap file in _initViewSettings() method then it worked.

    $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper(‘viewRenderer’);
    $viewRenderer->initView();

    //add the global helper directory path
    $viewRenderer->view->addHelperPath(APPLICATION_PATH.’/views/helpers’);

  • Viren

    Hello Penuel

    Can you please give me basic information on zend so that i can start learning and working on it

    Thanks
    Viren

  • http://rapsli.ch Raphael

    Thanks. Finally one that worked… maybe I was just not smart enough with the others.