[英]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. 我有一个名为Zend_View_Helper_FormVars
的助手,我的一个模块使用过它。 I also have a common helper in application/common/helpers/GeneralFunctions.php
我在application/common/helpers/GeneralFunctions.php
也有一个共同的帮助器
I'm trying to call a function from Zend_View_Helper_FormVars
that's in GeneralFunctions.php
. 我正在尝试从GeneralFunctions.php
Zend_View_Helper_FormVars
调用一个函数。
Here is the short version of Zend_View_Helper_FormVars
: 这是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
: 这是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: 另外,忘了提一下我在这样的Bootstrap中自动加载了GeneralFunctions
帮助器,它已经可用于我的所有模块了:
$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 致命错误:第2行的/Applications/MAMP/htdocs/mysite/application/modules/dashboard/views/helpers/DashboardHelper.php中找不到“Common_Helper_General”类
It's actually really simple to call another View Helper. 调用另一个View Helper实际上非常简单。
Make sure that your view helper extends Zend_View_Helper_Abstract
, so that it has access to the $view
. 确保您的视图助手扩展了Zend_View_Helper_Abstract
,以便它可以访问$view
。 Then you may simply call helpers as you would from a view, ie 然后你可以像在视图中那样简单地调用助手,即
$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;
}
}
You possibly haven't configured your autoloader to load classes from the application/common/helpers/
folder. 您可能尚未将自动加载器配置为从application/common/helpers/
文件夹加载类。
See Zend_Application_Module_Autoloader
for default paths. 有关默认路径,请参阅Zend_Application_Module_Autoloader
。 You should add your new folder to this. 您应该将新文件夹添加到此。
You are calling your class without instantiating it. 您正在调用您的类而不实例化它。
Your generalFunctions()
function uses the $this
pointer, which won't work; 你的generalFunctions()
函数使用$this
指针,它不起作用; 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. 另一种选择是首先实例化你的类。
I see several problems with your provided code. 我看到您提供的代码有几个问题。
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. 您正在尝试将Zend_View_Helper_GeneralFunctions::generalFunctions()
作为静态方法调用,因为它被声明为类方法(即您必须实例化该类的实例以使用它),因为您省略了static
关键字。 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. 如果你实际上想要使用generalFunctions()
作为静态方法并更正它,那么你需要使baseUrl
成为静态属性,或者你必须实例化该类的实例然后返回该实例。 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. 将GeneralFunctions
类用作直接调用的静态方法的容器的想法实际上是更深层问题的症状,并且正确标记为代码气味。 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). 如果您认为我在说谎,请查看Zend Framework 2.0的高优先级项目(提示:它涉及从框架中删除所有静态方法)。 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. 查看一般函数类Zend_View_Helper_GeneralFunctions
给定类名,并给出当前尝试在另一个帮助器中使用GeneralFunctions
帮助程序的情况,我猜想你真的需要做两件事之一。
GeneralFunctions
class so that all of your helpers have these functions available. 您需要将每个辅助类子类都包含在GeneralFunctions
类中,以便所有助手都可以使用这些函数。 Basically, ask yourself if your helpers all start out life as GeneralFunction
helpers with extended functionality beyond. 基本上,问问自己,你的助手是否都是以具有扩展功能的GeneralFunction
助手开始生活的。 This solution uses inheritance to solve your problem. 此解决方案使用继承来解决您的问题。 Every view helper should contain an instance of the View object being acted upon. 每个视图助手都应该包含要对其执行的View对象的实例。 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). 因此理论上你应该能够通过magic __call
方法访问任何其他视图助手(我认为还有一个显式方法,但我总是使用魔术方法)。 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. 在这种情况下, __call
call方法将加载GeneralFunctions
帮助程序,然后从GeneralFunctions
帮助程序调用progressMeter()
方法。
Now your GeneralFunctions
helper class would probably look like this: 现在您的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; } }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.