php 如何添加视图助手目录(zend 框架)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2335545/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add a view helper directory (zend framework)
提问by Cédric Girard
I begin with ZF (1.9.7), and I want to use View Helpers from a library shared between all my projects. But I can't find how to add it directory to the helpers path. My herpers works fines when I put them in application's helpers path.
我从 ZF (1.9.7) 开始,我想使用我所有项目之间共享的库中的查看助手。但我找不到如何将它的目录添加到助手路径。当我将它们放入应用程序的助手路径时,我的 Herpers 工作正常。
Here is the error, where I find the path to ZF helpers, and path to the applications ones.
这是错误,我在其中找到 ZF 帮助程序的路径和应用程序的路径。
object(ArrayObject)#71 (3) {
["exception"]=>
object(Zend_Loader_PluginLoader_Exception)#70 (6) {
["message:protected"]=>
string(151) "Plugin by name 'Voo' was not found in the registry; used paths:
Zend_View_Helper_: Zend/View/Helper/;C:/ZendStd/www/applis/VOO4_PROJECTX/views\helpers/"
["string:private"]=>
string(0) ""
["code:protected"]=>
int(0)
["file:protected"]=>
string(89) "C:\Program Files\Zend\ZendServer\share\ZendFramework\library\Zend\Loader\PluginLoader.php"
["line:protected"]=>
int(401)
Best regards
Cédric
最好的问候
塞德里克
回答by David Snabel-Caunt
It can be done very easily with the built in Zend_Application resource for the view. If you're using ini configs, add a line like this:
使用视图的内置 Zend_Application 资源可以很容易地完成。如果您使用的是 ini 配置,请添加如下一行:
resources.view.helperPath.My_View_Helper = "My/View/Helper"
The end of the key is the class name prefix, and the value the path where they reside.
键的末尾是类名前缀,值是它们所在的路径。
回答by Gordon
Helper paths are added through Zend_View_Abstract::addHelperPath(). You can call this method directly on an existing View instance.
辅助路径通过Zend_View_Abstract::addHelperPath() 添加。您可以直接在现有 View 实例上调用此方法。
Helper paths can also be configured in various ways during bootstrap. Check out the ZF manual chapter on Zend_Applicationto see how to use Bootstrap classes and resources:
辅助路径也可以在引导期间以各种方式配置。查看 ZF 手册章节Zend_Application,了解如何使用 Bootstrap 类和资源:
- http://framework.zend.com/manual/1.12/en/zend.view.helpers.html
- http://framework.zend.com/manual/1.12/en/zend.application.quick-start.html
- http://framework.zend.com/manual/1.12/en/zend.application.examples.html
- http://framework.zend.com/manual/1.12/en/zend.application.available-resources.html#zend.application.available-resources.view
- http://framework.zend.com/manual/1.12/en/zend.view.helpers.html
- http://framework.zend.com/manual/1.12/en/zend.application.quick-start.html
- http://framework.zend.com/manual/1.12/en/zend.application.examples.html
- http://framework.zend.com/manual/1.12/en/zend.application.available-resources.html#zend.application.available-resources.view
回答by Ali Sed
There is a problem when using
使用时出现问题
resources.view.helperPath.App_View_Helper = APPLICATION_PATH "/../library/App/views/helpers"
I can access no view helper in the layout even local helpers in the module. (Plugin by name 'LoggedInAs' was not found in the registry) but still working in views template files.
我无法访问布局中的任何视图助手,甚至模块中的本地助手。(在注册表中找不到名称为“LoggedInAs”的插件)但仍在视图模板文件中工作。
I put this code "echo Zend_Debug::dump($this)" at the end of layout file and there is a part of output.
我将此代码“echo Zend_Debug::dump($this)”放在布局文件的末尾,并且有一部分输出。
["_prefixToPaths:protected"] => array(3) {
["Zend_View_Helper_"] => array(2) {
[0] => string(17) "Zend/View/Helper/"
[1] => string(16) "./views\helpers/"
}
["ZendX_JQuery_View_Helper_"] => array(1) {
[0] => string(25) "ZendX/JQuery/View/Helper/"
}
["Zend_View_Helper_Navigation_"] => array(1) {
[0] => string(28) "Zend/View/Helper/Navigation/"
}
}
but when using these code in the bootstrap file there is no problem.
但是在引导文件中使用这些代码时没有问题。
//Initialize and/or retrieve a ViewRenderer object on demand via the helper broker
$viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
$viewRenderer->initView();
//add the global helper directory path
$viewRenderer->view->addHelperPath(APPLICATION_PATH.'/../library/App/views/helpers', 'App_View_Helper');
the output was like below:
输出如下:
["_prefixToPaths:protected"] => array(4) {
["Zend_View_Helper_"] => array(3) {
[0] => string(17) "Zend/View/Helper/"
[1] => string(16) "./views\helpers/"
[2] => string(86) "D:/zf/application/modules/default/views\helpers/"
}
["App_View_Helper_"] => array(1) {
[0] => string(85) "D:\zf\application/../library/App/views/helpers/"
}
["ZendX_JQuery_View_Helper_"] => array(1) {
[0] => string(25) "ZendX/JQuery/View/Helper/"
}
["Zend_View_Helper_Navigation_"] => array(1) {
[0] => string(28) "Zend/View/Helper/Navigation/"
}
}
回答by General Redneck
EDIT: Check out view helper in zend frameworkfor a more detailed take on this issue using rob allen's Loggedinas view helper.
编辑:使用 rob allen 的 Loggedinas 视图助手查看 zend 框架中的视图助手,以更详细地了解此问题。
Not only can you do as specified by David Caunt, but you can also do it like this in your bootstrap. Note there is always more than one way to do anything in Zend Framework
您不仅可以按照 David Caunt 的规定进行操作,而且还可以在引导程序中这样做。请注意,在 Zend Framework 中做任何事情的方法都不止一种
Check out http://devzone.zend.com/article/3412
查看http://devzone.zend.com/article/3412
If you have access to the view object, do the following.
如果您有权访问视图对象,请执行以下操作。
<?php
$view->addHelperPath('My/View/Helper/', 'My_View_Helper');
?>
you may need to obtain the view object if you in a front controller plugin
如果您在前端控制器插件中,您可能需要获取视图对象
Also Check out this really great set of posts starting here:
还可以从这里开始查看这组非常棒的帖子:
回答by Penuel
I have written short simple tutorial for registering the zend view helpers from a common directory, which can be accessed throughout the application. Please have a look.
我编写了简短的教程,用于从公共目录注册 zend 视图助手,该目录可以在整个应用程序中访问。请看一看。
http://www.mixedwaves.com/2010/03/accessing-and-using-zend-view-helpers-from-a-common-directory/
http://www.mixedwaves.com/2010/03/accessing-and-using-zend-view-helpers-from-a-common-directory/

