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.
- 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 thestatic
keyword. - If you in fact want to use
generalFunctions()
as a static method and correct this then you will need to either makebaseUrl
a static property or you will have to instantiate an instance of the class and then return that instance. - 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.
- 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 asGeneralFunction
helpers with extended functionality beyond. This solution uses inheritance to solve your problem. 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 theGeneralFunctions
helper and would then would call theprogressMeter()
method from theGeneralFunctions
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; }}