2

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

Ali
  • 603
  • 2
  • 6
  • 17

3 Answers3

4

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...
robertbasic
  • 4,325
  • 1
  • 27
  • 25
  • Yes, I _am_ using Zend_Layout. I'll try this method and see if it works, thank you :) – Ali Aug 22 '10 at 15:39
  • NICE!! It worked. Thank you! For anybody out there who needs to do this, I have a method called initView() in my Initializer class (bootstrap) that gets called in the routeStartup() phase with the following code: $layout = Zend_Layout::getMvcInstance(); $view = $layout->getView(); $view->addHelperPath(PATH_VIEW_HELPERS, 'My_View_Helper_'); Thanks again @robertbasic :) – Ali Aug 22 '10 at 16:27
0

Im not sure understand your Problem but, in my opinion you could register the view Object in Zend_Registry

Zend_Registry::set('view', new Your_view);
streetparade
  • 32,000
  • 37
  • 101
  • 123
  • Thanks man, my problem is that I can't access the view object in my bootstrap file. I'm not trying to create a new view of my own, I just want access to the view that my controller is going to be using, so that I can add the view helper to it. – Ali Aug 19 '10 at 22:05
0

You can do the following in your "setup.php / bootstrap.php or whatever file"

$view = new Zend_View();
// add Helper Path 
$view->setHelperPath('/path/to/more/helpers', 'My_View_Helper');
opHASnoNAME
  • 20,224
  • 26
  • 98
  • 143
  • Thanks ArneRie, but unfortunately that only sets the helper path of that particular instance of Zend_View that you just created. Unfortunately that doesn't work. – Ali Aug 22 '10 at 16:29