Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zend Framework call view helper from a Zend_View_Helper

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

I'm trying to call a function from Zend_View_Helper_FormVars that's in GeneralFunctions.php.

Here is the 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, forgot to mention that I have the GeneralFunctions helper auto loaded in the Bootstrap like this and it's available to all my modules already:

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

Here is what I tried, but am 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 I get:

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

like image 244
EricP Avatar asked Nov 25 '09 22:11

EricP


1 Answers

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:

<?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;
    }
}
like image 69
David Snabel-Caunt Avatar answered Nov 18 '22 00:11

David Snabel-Caunt