8

我有一个助手Zend_View_Helper_FormVars,它被我的一个模块使用。我也有一个共同的帮手application/common/helpers/GeneralFunctions.php

我正在尝试从中调用一个Zend_View_Helper_FormVars函数 GeneralFunctions.php

这是 的简短版本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;
    }
}

这是中的代码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;
    }
}

另外,忘了提到我已经GeneralFunctions像这样在 Bootstrap 中自动加载了帮助程序,并且它已经可用于我的所有模块:

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

这是我尝试过的,但出现错误:

// 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();
//-------------------->

我得到的错误信息:

致命错误:在第 2 行的 /Applications/MAMP/htdocs/mysite/application/modules/dashboard/views/helpers/DashboardHelper.php 中找不到类“Common_Helper_General”

4

4 回答 4

14

调用另一个 View Helper 其实很简单。

确保您的视图助手扩展Zend_View_Helper_Abstract,以便它可以访问$view. 然后,您可以像从视图中一样简单地调用助手,即

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

根据您上面的示例:

<?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;
    }
}
于 2009-11-27T18:10:12.480 回答
0

您可能尚未将自动加载器配置为从application/common/helpers/文件夹加载类。

查看Zend_Application_Module_Autoloader默认路径。您应该将新文件夹添加到此。

于 2009-11-26T02:15:49.970 回答
0

您正在调用您的类而不实例化它。

你的generalFunctions()函数使用了$this指针,它不起作用;它也不是静态方法。

一个选项是将进度表设置为静态函数并像这样直接调用它:

Zend_View_Helper_GeneralFunctions::progressMeter();

另一种选择是首先实例化您的类。

于 2009-11-26T04:50:49.317 回答
0

我发现您提供的代码存在几个问题。

  1. 由于省略了关键字Zend_View_Helper_GeneralFunctions::generalFunctions(),当它被声明为类方法时(即,您必须实例化类的实例才能使用它),您试图将其作为静态方法调用。static
  2. 如果您实际上想generalFunctions()用作静态方法并更正此问题,那么您将需要创建baseUrl一个静态属性,或者您必须实例化该类的一个实例,然后返回该实例。
  3. GeneralFunctions将您的类用作直接调用的静态方法的容器的想法实际上是更深层次问题的症状,并且被正确地标记为代码异味。如果您认为我在撒谎,请查看 Zend Framework 2.0 的高优先级项目(提示:它涉及从框架中删除所有静态方法)。或者你总是可以问他们对静态方法的看法:-)。

查看您给定的通用函数类的类名,Zend_View_Helper_GeneralFunctions并考虑到您尝试GeneralFunctions在另一个帮助器中使用帮助器的当前场景,我推测您确实需要做两件事之一。

  1. 您需要让每个助手类都成为该类的子GeneralFunctions类,以便您的所有助手都可以使用这些功能。基本上,问问你自己,你的助手是否都是从GeneralFunction具有扩展功能的助手开始的。此解决方案使用继承来解决您的问题。
  2. 每个视图助手都应该包含一个被操作的视图对象的实例。因此,理论上您应该能够通过魔术__call方法访问任何其他视图助手(我认为还有一个显式方法,但我总是使用魔术方法)。在您的场景中可能看起来像这样:

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

    在这种情况下,该__call方法将加载GeneralFunctions帮助程序,然后将从帮助程序调用该progressMeter()方法GeneralFunctions

    现在你的GeneralFunctions助手类可能看起来像这样:

    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;
        }
    }
    
于 2009-11-26T05:46:53.930 回答