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.