Would be the benefit or disadvantage of using an action controller instead? Is it only that the action controller plugin might be called multiple times?

On 7/1/2010 8:58 AM, Matthew Weier O'Phinney wrote:
-- debussy007<debussy...@gmail.com>  wrote
(on Thursday, 01 July 2010, 01:07 AM -0700):
Hi, I have a module called let's say products. In every view file of module
products I need some common variables. Thus I want to avoid having the same
code over and over in every action of products module to set those variables.
So I made a Plugin and in its preDispatch function I check if a request is made
for the module products: if($request->getModuleName() === 'products') { // TODO
} Now I want to set some variables for the view files, but how ? (PS: I don't
make a ViewHelper as I don't need a common code snippet for the views, I really
need some variables which I'd use here and there in the view files of products
module). Thank you for any help !!
You're on the right track. What you need to do is get the View object.
This will be available in one of two ways.

  * Should always work, unless you are not using the ViewRenderer:

    $vr = Zend_Controller_Action_HelperBroker::getStaticHelper('ViewRenderer');
    $view = $vr->view;

  * If you're enabling the "view" application resource, you can grab it
    from the bootstrap:

    $front = $this->getFrontController();
    $bootstrap = $front->getParam('bootstrap');
    $view = $bootstrap->getResource('view');

Put one of these in your plugin method, and you're ready to go:

     class Some_Plugin extends Zend_Controller_Plugin_Abstract
     {
         public function preDispatch(Zend_Controller_Request_Abstract $request)
         {
             if ($request->getModuleName() != 'products') {
                 return; // bail if wrong module
             }

             $front = $this->getFrontController();
             $bootstrap = $front->getParam('bootstrap');
             $view = $bootstrap->getResource('view');

             $view->assign(array(
                 // some variables...
             ));

             // done
         }
     }

Reply via email to