在我自己的 abstact 库(Zend Framework)中返回控制器操作


Get returned of controller action in my own abstact library (Zend Framework)

您好,我正在尝试在Zend框架的调度方法之一中检索我自己的库抽象中的控制器操作返回的值,我想知道这一壮举是否可能,如果是,该怎么做。

我的代码如下:

索引控制器

class IndexController extends My_Controller
{
    public function init()
    {
        /* Initialize action controller here */
    }
    public function indexAction()
    {
       // action body
        return 'hello world';
    }

}

My_Controller

abstract class My_Controller extends Zend_Controller_Action
{
    /**
     * Initialize Core_Controller
     * @param Zend_Controller_Request_Abstract $request
     * @param Zend_Controller_Response_Abstract $response
     * @param array $invokeArgs
     */
    public function __construct(Zend_Controller_Request_Abstract $request, Zend_Controller_Response_Abstract $response, array $invokeArgs = array())
    {
        parent::__construct($request, $response, $invokeArgs);
        $this->_helper->viewRenderer->setNoRender();
    }
    public function preDispatch()
    {
        //something here
    }
    public function postDispatch()
    {
        //something here
    }

    public function dispatch()
    {
        //something here
    }
}

我需要获取此库中控件中返回的值,以便将其转换为 json,然后打印到屏幕。

在 ZF 1 中,无法从控制器操作中获取返回值。 Zend Framework 本身永远不会使用或捕获此值。

查看Zend/Controller/Action.php行 516 (ZF 1.11.11),这是 ZF 调用控制器操作的点,并且不会捕获或使用返回值。

public function dispatch($action)
{
    // Notify helpers of action preDispatch state
    $this->_helper->notifyPreDispatch();
    $this->preDispatch();
    if ($this->getRequest()->isDispatched()) {
        if (null === $this->_classMethods) {
            $this->_classMethods = get_class_methods($this);
        }
        // If pre-dispatch hooks introduced a redirect then stop dispatch
        // @see ZF-7496
        if (!($this->getResponse()->isRedirect())) {
            // preDispatch() didn't change the action, so we can continue
            if ($this->getInvokeArg('useCaseSensitiveActions') || in_array($action, $this->_classMethods)) {
                if ($this->getInvokeArg('useCaseSensitiveActions')) {
                    trigger_error('Using case sensitive actions without word separators is deprecated; please do not rely on this "feature"');
                }
                $this->$action(); // <--- line 516 - this calls your action
            } else {
                $this->__call($action, array());
            }
        }
        $this->postDispatch();
    }
    // whats actually important here is that this action controller is
    // shutting down, regardless of dispatching; notify the helpers of this
    // state
    $this->_helper->notifyPostDispatch();
}

如您所见,控制器返回的值永远不会被使用。 此外,在 ZF2 中,它们正在更改控制器操作的工作方式,因此返回值实际上具有意义,因此您可能需要考虑不同的方法。

目前我能想到的最快的事情是,不要尝试从控制器返回值,而只需设置一个稍后可以获取的注册表值。

例如

public function returnAction()
{
    // ...
    Zend_Registry::set('controller_return_value', 'hello world');
}

然后稍后,在您的插件中或您尝试获取值的任何地方:

try {
    $retval = Zend_Registry::get('controller_return_value');
} catch (Zend_Exception $ex) {
    $retval = null; // no return value set by controller
}