Zend framework2 ZF2-转发插件返回ViewModel对象。如何使其返回其他值,例如简单数组或关联数组?
我从一个控制器的动作方法调用向前插件,从另一个控制器的动作方法获取值:Zend framework2 ZF2-转发插件返回ViewModel对象。如何使其返回其他值,例如简单数组或关联数组?,zend-framework2,zend-framework-mvc,zend-framework-routing,Zend Framework2,Zend Framework Mvc,Zend Framework Routing,我从一个控制器的动作方法调用向前插件,从另一个控制器的动作方法获取值: 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
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'
));
}
}
以下是我申请的Boo
控制器:
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',
);
}
}
如果Iprint\r($result)
它就是error/404
页面的视图模型:
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] =>
)
发生了什么事?如何更改此行为并从转发插件获取所需的数据类型
UPD 1
目前只找到以下内容:
MVC为控制器注册了两个监听器以实现自动化
这第一个将查看是否返回了关联数组
从你的控制器;如果是这样,它将创建一个视图模型并
变量容器的关联数组;那么这个视图模型呢
替换MvcEvent的结果
但这不起作用:
$this->getEvent()->setResult(array(
'one' => 'value one',
'two' => 'value two',
'three' => 'value three',
));
return $this->getEvent()->getResult(); // doesn't work, returns ViewModel anyway
这意味着我不必只获取一个数组,而必须将变量放入
ViewModel
,返回ViewModel
,然后从ViewModel
获取这些变量。非常好的设计,我可以说。您必须在ZF2中的操作中禁用视图。您可以这样做:
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;
}
}