views:

68

answers:

3

Assuming you're trying to keep yourself as close as possible to the Zend Framework view of the universe, how should view and action helpers from one module be shared with another module?

Let's say I have an with two modules. Module "A" has a view helper named Output.

class Modulea_Zend_View_Helper_Ouput extends Zend_View_Helper_Abstract
{
    function output($var)
    {
        echo strip_tags($var);
        return true;
    }
}

If I attempt to use this helper from a view in Module "B"

File: moduleb/views/scripts/index/index.phtml

<?php $this->output($somevar); ?>

I get an exception

Plugin by name 'Output' was not found in the registry

What's the "right" was to use the view helper Output from Module B.

A: 

i actually have a library folder and inside it view and helper folders. i put common helpers in this folder.

my structure:

-application
  --modules
   ---modulea
   ---moduleb
-library
  --view
   ---HELPER

i have added library folder to my include path using set_include_path command.

rahim asgari
That's a decent solution Rahim, but I'm not sure it's the "right" one. The Zend_Framework has always struck me as something that's meant to sit on top of PHP itself, and abstract away the need to use core functions like set_include_path to handle loading in your code.
Alan Storm
+1  A: 

You could add the helper to your view in the bootstrap

    $this->bootstrap('view');
    $view->addHelperPath(APPLICATION_PATH . '/views/helpers/','My_View_Helper');
Chellini
+1  A: 

If you put your helpers in library path, they will be autoloaded anywhere.

library
   Zend
     View
       Helper
         YourHelper.php
   Zend
     Controller
       Action
         Helper
           YourHelper.php

You may replace Zend with your own namespace, but you have to set up it, eg. in application.ini:

resources.view.helperPath.Application_View_Helper = "Application/View/Helper"
resources.frontController.actionHelperPaths.Application_Controller_Action_Helper = "Application/Controller/Action/Helper"
takeshin
It is strongly recommended to never use the `Zend_` prefix yourself. It could potentially make for collisions and unexpected behaviour during future upgrades.
jason