calling action helper in a plugin
In my controller/action, I can call an action helper
$this->_helper->getHelper('layout')->disableLayout();
I wa开发者_开发百科nt to do the same thing in a plugin, so I tried this but it doesn't work. How can a plugin access an action helper?
$controller = $this->getRequest()->getControllerName();
$controller->getHelper('layout')->disableLayout();
Method getControllerName
return controller's name, not object. In the front controller plugin, you don't have access to controller object.
If you wanna access action helper in plugin, you should use Zend_Controller_Action_HelperBroker
and its getStaticHelper()
method:
There are also two static methods for retrieving helpers from the helper broker: getExistingHelper() and getStaticHelper(). getExistingHelper() will retrieve a helper only if it has previously been invoked by or explicitly registered with the helper broker; it will throw an exception if not. getStaticHelper() does the same as getExistingHelper(), but will attempt to instantiate the helper if has not yet been registered with the helper stack. getStaticHelper() is a good choice for retrieving helpers which you wish to configure.
So your code should look like:
$layout = Zend_Controller_Action_HelperBroker::getStaticHelper('Layout');
$layout->disableLayout();
精彩评论