0
votes

I call the Forward plugin from one Controller's action method to get the value from the other Controller's action method:

namespace Foo/Controller;

class FooController {

    public function indexAction() {

        // I expect the $result to be an associative array,
        //    but the $result is an instance of the Zend\View\Model\ViewModel
        $result = $this->forward()->dispatch('Boo/Controller/Boo', 
                                              array(
                                                  'action' => 'start'
                                             ));
    }
}

And here's Boo Controller I apply to:

namespace Boo/Controller;

class BooController {

    public function startAction() {

        // I want this array to be returned,
        //     but an instance of the ViewModel is returned instead
        return array(
            'one' => 'value one',
            'two' => 'value two',
            'three' => 'value three',
        );
    }
}

And if I print_r($result) it is the ViewModel of the error/404 page:

Zend\View\Model\ViewModel Object
(
    [captureTo:protected] => content
    [children:protected] => Array
        (
        )

    [options:protected] => Array
        (
        )

    [template:protected] => error/404
    [terminate:protected] => 
    [variables:protected] => Array
        (
            [content] => Page not found
            [message] => Page not found.
            [reason] => error-controller-cannot-dispatch
        )

    [append:protected] => 
)

What is going on? How to change this behavior and get the required data type from the Forward plugin?

UPD 1

For now found only this here:

The MVC registers a couple of listeners for controllers to automate this. The first will look to see if you returned an associative array from your controller; if so, it will create a View Model and make this associative array the Variables Container; this View Model then replaces the MvcEvent‘s result.

And this doesn't work:

$this->getEvent()->setResult(array(
                'one' => 'value one',
                'two' => 'value two',
                'three' => 'value three',
            ));

return $this->getEvent()->getResult();  // doesn't work, returns ViewModel anyway

It means instead of to get just an array I have to put variable into a ViewModel, return a ViewModel and get those variable from the ViewModel. Very good design, I can say.

1

1 Answers

2
votes

You have to disable view in your action in ZF2. You can do this in this way:

namespace Application\Controller;

use Zend\Mvc\Controller\AbstractActionController;

class IndexController extends AbstractActionController
{
    public function indexAction()
    {
        $result = $this->forward()->dispatch('Application/Controller/Index', array( 'action' => 'foo' ));
        print_r($result->getContent());
        exit;
    }

    public function fooAction()
    {
        $response = $this->getResponse();
        $response->setStatusCode(200);
        $response->setContent(array('foo' => 'bar'));
        return $response;
    }
}