My two cents,
I've been using viewHelpers in this way:

A view helper for a specific controller goes in directory
app/modules/views/helpers/controllerName
A view helper shared between controllers in a given module goes in directory
app/modules/views/helpers/
A view helper shared by all modules goes in
app/views/helpers


Example:
Controller Specific viewHelpers
app/modules/default/views/helpers/index/IndexPage.phtml
app/modules/default/views/helpers/index/IndexPageForm.phtml
app/modules/default/views/helpers/login/LoginPage.php
app/modules/default/views/helpers/login/LoginPageForm.php
app/modules/default/views/helpers/login/GetMenu.php

ViewHelpers available to all controllers in default module:
app/modules/default/views/GetDefaultMenu.php

ViewHelpers available to all modules:
app/views/helpers/GetSideMenu.php
app/views/helpers/RouteOutput.php

In my acl plugin I add the view helper paths from general to specific.
With the  most specific being at the controller level.  Order is important..
$module = $request->module;
       $controller = $request->controller;
       $action     = $request->action;
$view = Zend_Registry::get('view'); $views_path = make_private_path("/app/modules/$module/views/scripts"); $view->addScriptPath($views_path); $views_path = make_private_path("/app/modules/$module/views/scripts/$controller");
       $view->addScriptPath($views_path);
$helperPath = make_private_path("/app/views/helpers"); $view->addHelperPath($helperPath); $helperPath = make_private_path("/app/modules/$module/views/helpers"); $view->addHelperPath($helperPath); $helperPath = make_private_path("/app/modules/$module/views/helpers/$controller"); $view->addHelperPath($helperPath);


So, in my IndexController I can write:

   public function indexAction() {

   $view = Zend_Registry::get('view');
   $text = $view->indexPage();   //call to viewHelper
   $view->routeOutput('main', $text);  //call to viewHelper
   $view->renderView();

}

Inside IndexPage.php.php I can write:

class Zend_View_Helper_IndexPage
{
   public function indexPage()
   {

   $view = Zend_Registry::get('view');
   $view->routeOutput('menu', $view->getMenu($flag));  //call to viewHelper
   return $view->indexPageForm();  // call to viewHelper

   }
}

class Zend_View_Helper_IndexPageForm
{
   public function indexPageForm()
   {

   $view = Zend_Registry::get('view');
   $form = blah...
   return $form;

   }
}
<?php
class Zend_View_Helper_RouteOutput
{
   public function routeOutput($to, $what, $overwrite = true) {
       $view = Zend_Registry::get('view');
       $route_map = array('main' =>'main_content', 'menu' =>'main_menu');
       $route = $route_map[$to];
               if(true === $overwrite)
           $view->$route = $what; //overwrite it
       else
$view->$route .= $what; // add to it }
}

?>

The amazing thing is it will actually find the proper ViewHelper. The view is
exo-polymorphic, you can add functions to it without touching the object or
mucking about with inheritance.

Sold on view helpers!


cheers,

pat

Reply via email to