ZF2:如何从其他Action方法完成请求然后请求来使用Forward Controller Plugin?

时间:2012-11-25 14:31:23

标签: zend-framework2 zend-framework-mvc zend-framework-routing

在ZF中,控制器中有一个Forward Plugin。手册说明了这一点:

  

有时,您可能希望从中调度其他控制器   在匹配的控制器中 - 例如,您可以使用它   建立“小组化”内容的方法。 Forward插件有帮助   启用此功能。

它的例子是这样的:

$foo = $this->forward()->dispatch('foo', array('action' => 'process'));

插件从foo控制器(FooController::processAction)返回到调用插件的初始匹配控制器。但是可以从foo控制器完成请求(向浏览器发送最终响应)吗?像这样:

class IndexController extends AbstractActionController {

    // The request comes here by routes
    public function indexAction() {

        if($someCondition) {

            // I forward the request to the fooAction of this IndexController
            // And I do not wait for any return from it, the response
            // will be sent from fooAction, the run will not come back here
            $this->forward()->dispatch('index', array('action' => 'foo'));
        }
    }

    // I want to send the response from this action and finish with the request
    public function fooAction() {

        $response = $this->getResponse();
        $response->setStatusCode(200);
        $response->setContent($someContent);

        // But it returns the response to the indexAction,
        // instead of sending it to browser.
        // How to finish the request here?
        return $response;
    }
}

是否可以使用Forward

1 个答案:

答案 0 :(得分:3)

使用常规的php return构造:

return $this->forward()->dispatch('index', array('action' => 'foo'));