zend-framework, call an action helper from within

2019-02-05 18:47发布

问题:

i am writing an action helper and i need to call another action helper from within that helper. but i dont know how. here in the sample code:

class Common_Controller_Action_Helper_SAMPLE extends Zend_Controller_Action_Helper_Abstract
{
    protected $_view;
    public function __construct(Zend_View_Interface $view = null, array $options = array())
    {
        $this->_view = $view;
    }

    public function preDispatch()
    {
        $flashMessenger = $this->_helper->FlashMessenger; // IT IS NULL
    }
}

回答1:

Use the action helper broker:

$flashMessenger =
    Zend_Controller_Action_HelperBroker::getStaticHelper('FlashMessenger');


回答2:

Another solution is:

$flashMessenger = $this->getActionController()->getHelper('FlashMessenger');


回答3:

You can also use getActionController to get a reference back to the actioncontroller you were using for any methods you'd normally use there.



回答4:

In addition to mercator's answer, add your method after, see example below:

Zend_Controller_Action_HelperBroker::getStaticHelper('FlashMessenger')->myMethod();


回答5:

You can call it in this way:

$this->_actionController->OtherActionHelper();

The _actionController property references the actual action controller.