ZEND:无法注册视图助手
ZEND: Cannot register view helper
我已经花了一整天了,但我无法让它工作。
我有这个自定义视图助手 class:
class ShowPopupMessages extends Zend_View_Helper_Abstract {
public function showPopupMessages()
{
$flashMessenger = Zend_Controller_Action_HelperBroker::getStaticHelper('FlashMessenger');
if($flashMessenger->hasMessages()) {
$msg =$flashMessenger->getMessages()[0];
return "ShowToastMessage('$msg','success')";
}
}
}
附上我项目的文件结构图
我正在尝试在我的 Bootstrap.php 中注册这个 class:
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap {
protected function _initPlaceholders()
{
$this->bootstrap('view');
$view = $this->getResource('view');
$view->doctype('XHTML1_STRICT');
$view->addHelperPath(
APPLICATION_PATH.'/views/helpers/', 'ShowPopupMessages'
);
$helper = new ShowPopupMessages();
$view->registerHelper($helper, 'showPopupMessages');
}
这是我的 application.ini:
[production]
phpSettings.display_startup_errors = 0
phpSettings.display_errors = 0
includePaths.library = APPLICATION_PATH "/../library"
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"
appnamespace = "Application"
resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
resources.frontController.params.displayExceptions = 0
resources.view.doctype = "XHTML1_STRICT"
resources.view.helperPath = APPLICATION_PATH "/views/helpers"
但我不断得到:
Fatal error: Class 'ShowPopupMessages' not found in
C:\xampp\htdocs\Platforma\application\Bootstrap.php on line 19
class名字是不是错了?放在树上?
我能以某种方式使它自动加载吗(编写这么多代码来注册每个助手是一种疯狂!)?
如果是这样,将其放在项目树中的什么位置?
抱歉,Zend 文档写得太糟糕了,我无法从中学习。
感谢和问候!
汤姆
尝试:
在application.ini
resources.view.helperPath.My_View_Helper = APPLICATION_PATH "/views/helpers"
和class姓名:My_View_Helper_ShowPopupMessages
按照您的提示,我终于明白了:
可能class名字是
class My_View_Helper_ShowPopupMessages extends Zend_View_Helper_Abstract
助手文件名是:
ShowPopupMessages.php
它被放置在 \views\helpers
在 bootstrap.php 我补充说:
protected function _initViewHelperPaths()
{
$this->bootstrap('view');
$view = $this->getResource('view');
$view->addHelperPath(APPLICATION_PATH . '/views/helpers', 'My_View_Helper_');
}
并且不需要显式注册每个自定义助手:
$view->addHelperPath(APPLICATION_PATH . '/views/helpers', 'My_View_Helper_');
完成了所有工作,不需要再弄乱 application.ini(至少在我的简单应用程序中)
感谢和问候
汤姆
我已经花了一整天了,但我无法让它工作。
我有这个自定义视图助手 class:
class ShowPopupMessages extends Zend_View_Helper_Abstract {
public function showPopupMessages()
{
$flashMessenger = Zend_Controller_Action_HelperBroker::getStaticHelper('FlashMessenger');
if($flashMessenger->hasMessages()) {
$msg =$flashMessenger->getMessages()[0];
return "ShowToastMessage('$msg','success')";
}
}
}
附上我项目的文件结构图
我正在尝试在我的 Bootstrap.php 中注册这个 class:
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap {
protected function _initPlaceholders()
{
$this->bootstrap('view');
$view = $this->getResource('view');
$view->doctype('XHTML1_STRICT');
$view->addHelperPath(
APPLICATION_PATH.'/views/helpers/', 'ShowPopupMessages'
);
$helper = new ShowPopupMessages();
$view->registerHelper($helper, 'showPopupMessages');
}
这是我的 application.ini:
[production]
phpSettings.display_startup_errors = 0
phpSettings.display_errors = 0
includePaths.library = APPLICATION_PATH "/../library"
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"
appnamespace = "Application"
resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
resources.frontController.params.displayExceptions = 0
resources.view.doctype = "XHTML1_STRICT"
resources.view.helperPath = APPLICATION_PATH "/views/helpers"
但我不断得到:
Fatal error: Class 'ShowPopupMessages' not found in C:\xampp\htdocs\Platforma\application\Bootstrap.php on line 19
class名字是不是错了?放在树上? 我能以某种方式使它自动加载吗(编写这么多代码来注册每个助手是一种疯狂!)? 如果是这样,将其放在项目树中的什么位置?
抱歉,Zend 文档写得太糟糕了,我无法从中学习。
感谢和问候!
汤姆
尝试:
在application.ini
resources.view.helperPath.My_View_Helper = APPLICATION_PATH "/views/helpers"
和class姓名:My_View_Helper_ShowPopupMessages
按照您的提示,我终于明白了:
可能class名字是
class My_View_Helper_ShowPopupMessages extends Zend_View_Helper_Abstract
助手文件名是:
ShowPopupMessages.php
它被放置在 \views\helpers
在 bootstrap.php 我补充说:
protected function _initViewHelperPaths()
{
$this->bootstrap('view');
$view = $this->getResource('view');
$view->addHelperPath(APPLICATION_PATH . '/views/helpers', 'My_View_Helper_');
}
并且不需要显式注册每个自定义助手:
$view->addHelperPath(APPLICATION_PATH . '/views/helpers', 'My_View_Helper_');
完成了所有工作,不需要再弄乱 application.ini(至少在我的简单应用程序中)
感谢和问候
汤姆