Zend Framework call view helper from a Zend_View_Helper Zend Framework call view helper from a Zend_View_Helper php php

Zend Framework call view helper from a Zend_View_Helper


It's actually really simple to call another View Helper.

Make sure that your view helper extends Zend_View_Helper_Abstract, so that it has access to the $view. Then you may simply call helpers as you would from a view, i.e.

$this->view->generalFunctions()->progressMeter();

Based on your example above:

<?phpclass Zend_View_Helper_FormVars extends Zend_View_Helper_Abstract {    /* ... */    public function mkCategoryCodeSelectGroup($codeTypeArr=array(),        $codesArr=array()) {        $html='';        $html. $this->view->generalFunctions()->progressMeter();        return $html;    }}


You possibly haven't configured your autoloader to load classes from the application/common/helpers/ folder.

See Zend_Application_Module_Autoloader for default paths. You should add your new folder to this.


I see several problems with your provided code.

  1. You are attempting to call Zend_View_Helper_GeneralFunctions::generalFunctions() as a static method when it is declared as a class method (ie you have to instantiate an instance of the class to use it) by reason of your omission of the static keyword.
  2. If you in fact want to use generalFunctions() as a static method and correct this then you will need to either make baseUrl a static property or you will have to instantiate an instance of the class and then return that instance.
  3. The idea of using your GeneralFunctions class as a container for static methods that are called directly is really a symptom of deeper problems and is rightly labeled a code smell. If you think that I'm lying take a look at the high priority items for the Zend Framework 2.0 (hint: it involves removing all static methods from the framework). Or you can always ask SO what they think of static methods :-).

Looking at your given class name for the general functions class Zend_View_Helper_GeneralFunctions and given the current scenario where you are trying to use the GeneralFunctions helper inside another helper, I would surmise that you really need to do one of two things.

  1. You need to have every helper class subclass the GeneralFunctions class so that all of your helpers have these functions available. Basically, ask yourself if your helpers all start out life as GeneralFunction helpers with extended functionality beyond. This solution uses inheritance to solve your problem.
  2. Every view helper should contain an instance of the View object being acted upon. Therefore in theory you should be able to access any other view helper via the magic __call method (I think there is also an explicit method but I always use the magic method). It might look like so in your scenario:

    public function mkCategoryCodeSelectGroup($codeTypeArr=array(), $codesArr=array()) {    $html='';    $html.= $this->generalFunctions()->progressMeter();    return $html;}

    In this scenario the __call method would load the GeneralFunctions helper and would then would call the progressMeter() method from the GeneralFunctions helper.

    Now your GeneralFunctions helper class would probably look like this:

    class Zend_View_Helper_GeneralFunctions{    public function __construct()    {        $this->baseUrl = Zend_Controller_Front::getInstance()->getBaseUrl();    }    public function progressMeter() {        $html='';        $html.='<span id="progressWrapper">';        $html.='<span id="progressMeter"></span>';        $html.='</span>';        $html.='';        return $html;    }}