Search code examples
zend-frameworkview-helpersactionviewhelper

Zend: how do I share common code between view and action helpers?


I need a boolean from a combination of some configuration and a call to an external webservice.

The boolean is needed in the layout file of every view. It is also needed in a select few controllers.

I have both an action and a view helper that get the required value from the external webservice.

Right now I have created both a view helper and an action helper with duplicate logic to check the external webservice and the configuration values, but it seems to me there should be a way to share that common code between helpers.

Is there a better way?

Thanks!


Solution

  • You should be able to perform both tasks with the action helper. You might want to take a look at this blog post from MWOP: http://mwop.net/blog/235-A-Simple-Resource-Injector-for-ZF-Action-Controllers where he uses an action helper to assign resources from the bootstrap to the controller. You'd want to take a similar approach except that you'd be setting your boolean variable on the controller. You also access the View object from within the helper using:

    $view = Zend_Layout::getMvcInstance()->getView();
    $view->yourBooleanVar = 'whatever';
    

    which you can then access in your views as normal.