Zend Framework 从 Zend_View_Helper 调用视图助手
我有一个名为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
调用另一个 View Helper 实际上非常简单。
确保您的视图助手扩展了
Zend_View_Helper_Abstract
,以便它可以访问$view
。然后,您可以像从视图中一样简单地调用助手,即根据上面的示例:
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.Based on your example above:
您可能尚未将自动加载器配置为从
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.我发现您提供的代码有几个问题。
Zend_View_Helper_GeneralFunctions::generalFunctions()
声明为类方法时将其作为静态方法调用(即,您必须实例化该类的实例才能使用它)static
关键字。查看通用函数类
Zend_View_Helper_GeneralFunctions
的给定类名,并考虑到您尝试在另一个助手中使用GeneralFunctions
助手的当前场景,我猜测您确实需要做两件事之一。GeneralFunction
助手开始。该解决方案使用继承来解决您的问题。每个视图助手都应该包含一个正在执行操作的视图对象的实例。因此,理论上,您应该能够通过魔术
__call
方法访问任何其他视图助手(我认为还有一个显式方法,但我总是使用魔术方法)。在您的场景中,它可能看起来像这样:在此场景中,
__call
方法将加载GeneralFunctions
帮助器,然后从调用
帮助器。progressMeter()
方法GeneralFunctions现在您的
GeneralFunctions
辅助类可能如下所示: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 thestatic
keyword.generalFunctions()
as a static method and correct this then you will need to either makebaseUrl
a static property or you will have to instantiate an instance of the class and then return that instance.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 theGeneralFunctions
helper inside another helper, I would surmise that you really need to do one of two things.GeneralFunctions
class so that all of your helpers have these functions available. Basically, ask yourself if your helpers all start out life asGeneralFunction
helpers with extended functionality beyond. This solution uses inheritance to solve your problem.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:In this scenario the
__call
method would load theGeneralFunctions
helper and would then would call theprogressMeter()
method from theGeneralFunctions
helper.Now your
GeneralFunctions
helper class would probably look like this:您正在调用您的类而不实例化它。
您的
generalFunctions()
函数使用$this
指针,这是行不通的;它也不是静态方法。一种选择是将进度表设置为静态函数并直接调用它,如下所示:
另一种选择是首先实例化您的类。
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:
Another option is to instantiate your class first.