简体   繁体   English

如何添加视图助手目录(zend框架)

[英]How to add a view helper directory (zend framework)

I begin with ZF (1.9.7), and I want to use View Helpers from a library shared between all my projects. 我从ZF(1.9.7)开始,我想从我所有项目之间共享的库中使用View Helpers。 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. 当我把它们放在应用程序的助手路径中时,我的牧师会罚款。

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 塞德里克

It can be done very easily with the built in Zend_Application resource for the view. 使用内置的Zend_Application资源可以非常轻松地完成视图。 If you're using ini configs, add a line like this: 如果您正在使用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. 密钥的末尾是类名前缀,以及它们所在的路径的值。

Helper paths are added through Zend_View_Abstract::addHelperPath() . 辅助路径通过Zend_View_Abstract :: addHelperPath()添加 You can call this method directly on an existing View instance. 您可以直接在现有View实例上调用此方法。

Helper paths can also be configured in various ways during bootstrap. 辅助路径也可以在引导期间以各种方式配置。 Check out the ZF manual chapter on Zend_Application to see how to use Bootstrap classes and resources: 查看Zend_Application的ZF手册章节,了解如何使用Bootstrap类和资源:

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. 但是在bootstrap文件中使用这些代码时没有问题。

    //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/"
              }
            }

EDIT: Check out view helper in zend framework for a more detailed take on this issue using rob allen's Loggedinas view helper. 编辑: 在zend框架中查看视图帮助程序,以便使用rob allen的Loggedinas视图帮助程序更详细地了解此问题。

Not only can you do as specified by David Caunt, but you can also do it like this in your bootstrap. 您不仅可以按照David Caunt的说明进行操作,还可以在引导程序中执行此操作。 Note there is always more than one way to do anything in Zend Framework 请注意,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: 从这里开始查看这个非常棒的帖子:

http://zend-framework-community.634137.n4.nabble.com/Getting-view-from-Bootstrap-ZF1-8-tp659447p659460.html http://zend-framework-community.634137.n4.nabble.com/Getting-view-from-Bootstrap-ZF1-8-tp659447p659460.html

I have written short simple tutorial for registering the zend view helpers from a common directory, which can be accessed throughout the application. 我编写了简短的简单教程,用于从公共目录注册zend视图助手,可以在整个应用程序中访问。 Please have a look. 请看一看。

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/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM