zend-framework2zend-framework-mvczend-framework-routing

ZF2: How to finish the request from the other Action method then the request came to using Forward Controller Plugin?


In ZF there is a Forward Plugin in controllers. The manual says about it:

Occasionally, you may want to dispatch additional controllers from within the matched controller – for instance, you might use this approach to build up “widgetized” content. The Forward plugin helps enable this.

The example of it is like this:

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

The plugin returns something from the foo controller (FooController::processAction) to the initial matched controller that calls the plugin. But is it possible to finish the request (send final response to browser) from that foo controller? Like this:

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;
    }
}

Is it possible with the Forward?


Solution

  • use regular php return construct:

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