View helpers in modules

I came across a situation last week where I needed to access a view helper that was in the default module's views/helpers directory when I was in another module. This came about because my layout.phtml uses a view helper that is in application/views/helpers.

By default, it doesn't work and you get an error along the lines of:

Plugin by name 'LoggedInAs' was not found in the registry; used paths: 
Plan_View_Helper_: /www/funkymongoose/habuplan/application/modules/plan/views/helpers/
Zend_View_Helper_: Zend/View/Helper/

The fix is simple enough. All you need to do is add this line to your application.ini file:

resources.view.helperPath.Zend_View_Helper = APPLICATION_PATH "/views/helpers"

This will then ensure that the view helpers in your application/views/helpers directory are always available.

This also works for accessing the view helpers in any other module too where the syntax looks like:

resources.view.helperPath.{Module}_View_Helper =
    APPLICATION_PATH "/modules/{module}/views/helpers"

Finally, don't forget that the order is important if you have view helpers of the same name in different modules. Personally, I would try to avoid that if you possibly can :)

5 thoughts on “View helpers in modules

  1. Is it not better to just upgrade the view helper to an application wide helper and add only that path (if it's not already included)?

    Like

    class App_View_Helper_Url extends Zend_View_Helper_Url

  2. Just tried this but accessing the module view helper from the default module but its not working.

    resources.view.helperPath.Admin_View_Helper = APPLICATION_PATH "/modules/admin/views/helpers"

    Message: Plugin by name 'Matrix' was not found in the registry; used paths: Zend_View_Helper_: Zend/View/Helper/:./views/helpers/:/var/www/vhosts/xxxx/olb/application/views/helpers/

    It seems that the module path has not been added? Any ideas?

  3. I'm seeing my plugins fail on a shared server setup. The error show's where the files are just like Jon's post above, but they don't load. I have resources.view.helperPath.Zend_View_Helper_ = APPLICATION_PATH "/views/helpers"
    in my ini too.

  4. I tried to use your solution.
    But when I put resources.view.helperPath.Zend_View_Helper = APPLICATION_PATH "/views/helpers" in application.ini
    and run project in browser I got following error and everything is stopped working. :(

    Fatal error: Uncaught exception 'Zend_Loader_PluginLoader_Exception' with message 'Plugin by name 'GetPath'

    Please guide me.
    Thanks.

Comments are closed.