Search code examples
zend-frameworkzend-viewview-helpers

Adding Zend View Helper to all views in application (in bootstrap)


I am using Zend Framework 1.6 hence I'm not utilizing Zend_Application.

I have a simple, normal View Helper (extending Zend_View_Helper_Abstract). It works perfectly fine as long as I add it to the view in my action controller. But I want to be able to use it in every view in every module. I figured it should be easy to just get the global view in my bootstrap file and do this:

$view->addHelperPath(PATH_VIEW_HELPERS, 'RT_View_Helper_');

But I can't seem to be able to get the $view object in my bootstrap file. I also tried

$viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
if (null === $viewRenderer->view) {
    $viewRenderer->initView();
}
$view = $viewRenderer->view; 
$view = new Zend_View(array('encoding'=>'UTF-8'));
$view->addHelperPath(PATH_VIEW_HELPERS, 'RT_View_Helper_');

But that doesn't do the trick either. I've tried putting it in the preDispatch() and postDispatch() of my boostrap (which is a front controller plugin).

Does anyone have any thoughts on how to do this? It seems it should be so simple, yet I haven't been able to find a solution for it in two days.

Thanks all :) Ali


Solution

  • If you are using Zend_Layout then something along these lines should get you the Zend_View:

    $layout = Zend_Layout::getMvcInstance();
    $view = $layout->getView();
    // set helpers, doctype, etc...