Zend Framework 从 Zend_View_Helper 调用视图助手

发布于 2024-08-13 03:30:26 字数 2950 浏览 7 评论 0原文

我有一个名为 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 帮助器 auto像这样加载到引导程序中,它已经可用于我的所有模块:

$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”

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

-小熊_ 2024-08-20 03:30:26

调用另一个 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;
    }
}

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;
    }
}
不交电费瞎发啥光 2024-08-20 03:30:26

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

请参阅 Zend_Application_Module_Autoloader 了解默认路径。您应该将新文件夹添加到其中。

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.

星星的軌跡 2024-08-20 03:30:26

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

  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 方法访问任何其他视图助手(我认为还有一个显式方法,但我总是使用魔术方法)。在您的场景中,它可能看起来像这样:

    公共函数 mkCategoryCodeSelectGroup($codeTypeArr=array(), $codesArr=array()) 
    {
        $html='';
        $html.= $this->generalFunctions()->progressMeter();
        返回$html;
    }
    

    在此场景中,__call 方法将加载 GeneralFunctions 帮助器,然后从 调用 progressMeter() 方法GeneralFunctions 帮助器。

    现在您的 GeneralFunctions 辅助类可能如下所示:

    类 Zend_View_Helper_GeneralFunctions
    {
        公共函数 __construct()
        {
            $this->baseUrl = Zend_Controller_Front::getInstance()->getBaseUrl();
        }
    
        公共函数进度表(){
            $html='';
            $html.='';
            $html.='';
            $html.='';
            $html.='';
            返回$html;
        }
    }
    

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;
        }
    }
    
浪菊怪哟 2024-08-20 03:30:26

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

您的 generalFunctions() 函数使用 $this 指针,这是行不通的;它也不是静态方法。

一种选择是将进度表设置为静态函数并直接调用它,如下所示:

Zend_View_Helper_GeneralFunctions::progressMeter();

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

You are calling your class without instantiating it.

Your generalFunctions() function uses the $this pointer, which won't work; also it isn't a static method.

One option is set progress meter to be a static function and call it directly like this:

Zend_View_Helper_GeneralFunctions::progressMeter();

Another option is to instantiate your class first.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文