19

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.

Here is the error, where I find the path to ZF helpers, and path to the applications ones.

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

5 Answers 5

38

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:

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.

1
11

Helper paths are added through Zend_View_Abstract::addHelperPath(). You can call this method directly on an existing View instance.

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:

2
  • 1
    Although all these options are correct, and will work, I feel the application.ini solution suggested by David to be the "best-practice". Also, in your "_initView()" you could use $this->bootstrap('view'); $view = $this->getResource('view'); to use the application.ini view instead of one you randomly create...
    – gnarf
    Feb 25, 2010 at 22:22
  • @gnarf with the exception of addHelperPath(), the bootstrap example is copied 1:1 from the ZF reference guide. It shouldn't be taken as anything else but an example. I am well aware there is different ways to do it (e.g. using an Ini). Feel free to do it differently.
    – Gordon
    Feb 25, 2010 at 22:41
5

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.

I put this code "echo Zend_Debug::dump($this)" at the end of layout file and there is a part of output.

        ["_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/"
              }
            }
1
  • Your solution worked like a charm! I was using helpers for the entire application and helpers for each module! This is what I was looking for! ;) Aug 25, 2015 at 2:32
1

EDIT: Check out view helper in zend framework for a more detailed take on this issue using rob allen's Loggedinas view helper.

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

Check out 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

0

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.

http://www.mixedwaves.com/2010/03/accessing-and-using-zend-view-helpers-from-a-common-directory/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.