views:

216

answers:

1

I have a controller plugin with postDispatch() hook, and there I have a $variable.

How to pass this variable to the view instance?

I tried Zend_Layout::getMvcInstance()->getView(), but this returns new view instance (not the application resource). The same with $bootstrap->getResource('view').

I don't want to pass it as a request param.
Now, as a workaround I do it using Zend_Registry.

But, is it the best way?

+1  A: 

I've been using the ViewRenderer action helper to get the view when I need it. It seems to be the most common way that Zend classes access the view object.

So, in the controller plugin:

class App_Controller_Plugin_ViewSetup extends Zend_Controller_Plugin_Abstract {

  public function postDispatch() {

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

    echo $view->variable;

    $view->variable = 'Hello, World';

  }

}

In the controller:

class IndexController extends Zend_Controller_Action {

  public function indexAction() {

    $this->view->variable = 'Go away, World';

  }

}

In the view script:

<?php echo $this->variable; ?>

The output is: Go away, WorldGo away, World

It seems like the problem is that the view script renders before the postDispatch() method is called, because this does return the main view object.

Robin Canaday
Tried this already. This returns new view instance too.
takeshin