What everyone's Zend_View_Helper tutorials leave out.

There seems to be several Zend_View_Helper tutorials out there and all of them talk about what a view helper is and does, but I always left with one question: How do I make the helpers available to ALL of my views?

Then I learned a bit about Zend_Controller_Action_HelperBroker

This class can work all kinds of magic, but the one thing we care about right now is that it will allow you to register a global View object to be passed to your Controller script. So all you have to do is create your own View and use addHelperPath() to tell it where your View helpers are and register your view with Controler_Action_HelperBroker

Just add the following bit of code to your front controller:

//Setup view helpers
Zend_Loader::loadClass('Zend_View');
$view = new Zend_View();
$helper_path = "/var/www/myhelpers";
$view->addHelperPath($helper_path,'View_Helper');
Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer')
  ->setView($view);
Advertisements