I wrote a plugin that needs to set a property on the controller that’s currently being dispatched. For example, if my plugin is:
class Application_Plugin_Foo extends Zend_Controller_Plugin_Abstract
{
public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request)
{
// Get an instance of the current controller and inject the $foo property
// ???->foo = 'foo';
}
}
I want to be able to do this:
class IndexController extends Zend_Controller_Action
{
public function indexAction()
{
$this->view->foo = $this->foo;
}
}
}
Any help is greatly appreciated!
The action controller is not directly accessible directly from a front-controller plugin. It’s the dispatcher that instantiates the controller object and he doesn’t appear to save it anywhere accessible.
However, the controller is accessible from any registered action helpers. Since action helpers have a
preDispatchhook, you could do your injection there.So, in
library/My/Controller/Helper/Inject.php:Then register an instance of the helper in
application/Bootstrap.php:And, as always, be sure to include
My_as an autoloader namespace inconfigs/application.ini:Then, in the controller, access the value directly as a public member variable:
One thing to note: Since the helper uses the
preDispatch()hook, I believe it will get called on every action, even an internalforward().