Zend Framework to call a view of a call from Zend_View_Helper

I have a Zend_View_Helper_FormVars helper that is used by one of my modules. I also have a general helper in application/common/helpers/GeneralFunctions.php

I am trying to call a function from Zend_View_Helper_FormVars , which is located in GeneralFunctions.php .

Here is a short version of Zend_View_Helper_FormVars :

 class Zend_View_Helper_FormVars { public $reqFieldVisual='<span class="req">*</span>'; public $roles=array('admin'=>'admin', 'user'=>'user'); public $paymentMethods=array('1'=>'Check', '2'=>'Credit Card', '3'=>'Cash', '4'=>'Other'); public function formVars(){ $this->baseUrl=Zend_Controller_Front::getInstance()->getBaseUrl(); return $this; } public function mkCategoryCodeSelectGroup($codeTypeArr=array(), $codesArr=array()) { $html=''; $html.=Zend_View_Helper_GeneralFunctions::generalFunctions()->progressMeter(); return $html; } } 

Here is the code in GeneralFunctions.php :

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

Also, I forgot to mention that I have a GeneralFunctions helper machine loaded in Bootstrap, like this one, and it is available for all my modules:

 $view->addHelperPath(APPLICATION_PATH .'/common/helpers', 'View_Helper'); 

Here is what I tried, but getting an error:

 // application/Bootstrap.php -----------> function _initViewHelpers() { // add a helper for use for all modules $view->addHelperPath(APPLICATION_PATH .'/Common/Helper', 'Common_Helper'); } //--------------------> // application/common/helpers/General.php -----------> class Zend_View_Helper_General extends Zend_View_Helper_Abstract { public function general(){ return $this; } public function test(){ return 'test 123'; } } //--------------------> // application/modules/dashboard/views/helpers/DashboardHelper.php -----------> class Zend_View_Helper_DashboardHelper extends Common_Helper_General { public function dashboardHelper(){ return $this; } public function dashboardTest(){ return 'from dashboard'; } } //--------------------> // application/modules/dashboard/views/scripts/index/index.phtml -----------> echo $this->dashboardHelper()->test(); //--------------------> 

Error message:

Fatal error: class 'Common_Helper_General' not found in /Applications/MAMP/htdocs/mysite/application/modules/dashboard/views/helpers/DashboardHelper.php on line 2

+7
php zend-framework zend-view
source share
4 answers

It is actually very simple to call another view helper.

Make sure your view helper extends Zend_View_Helper_Abstract so that it has access to $view . Then you can just call helpers, just like from a view, i.e.

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

Depending on your example above:

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

You may not have configured the autoloader to load classes from the application/common/helpers/ .

See Zend_Application_Module_Autoloader for default paths. You should add a new folder.

0
source share

You call your class without creating it.

The generalFunctions() function uses the $this pointer, which will not work; also it is not a static method.

One of the options is to set the progress bar as a static function and call it directly as follows:

 Zend_View_Helper_GeneralFunctions::progressMeter(); 

Another option is to instantiate the class first.

0
source share

I see several problems with your provided code.

  • You are trying to call Zend_View_Helper_GeneralFunctions::generalFunctions() as a static method when it is declared as a class method (i.e. you need to instantiate the class to use it) due to your lack of the static .
  • If you really want to use generalFunctions() as a static method and fix it, you need to either make baseUrl static property, or you will have to create an instance of the class and then return that instance.
  • The idea of ​​using your GeneralFunctions class as a container for static methods invoked directly is actually a symptom of deeper problems and is rightly indicated by the smell of code. If you think I'm lying, take a look at the high priority elements for Zend Framework 2.0 (hint: it includes removing all static methods from the framework). Or you can always ask what they think of static methods :-).

Looking at your class name for the Zend_View_Helper_GeneralFunctions common function class and given the current scenario in which you are trying to use the GeneralFunctions inside another helper, I would suggest that you really need to do one of two things.

  • There must be a GeneralFunctions class for each helper class so that all your helpers have these functions. Basically, ask yourself if your assistants have a life as GeneralFunction assistants with extended functionality beyond. This solution uses inheritance to solve your problem.
  • Each view helper must contain an instance of the View object on which the action is performed. Therefore, theoretically, you should have access to any other view helper using the __call magic method (I think there is also an explicit method, but I always use the magic method). In your scenario, it might look like this:

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

    In this case, the __call method loaded the GeneralFunctions and then would call the progressMeter() method from the GeneralFunctions .

    Now your helper class GeneralFunctions will 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; } } 
0
source share

All Articles