如何在 ZF2 中默认 return JsonModel?
How to return JsonModel by default in ZF2?
默认 ViewModel 不是强制性的,我可以 return 从控制器中获取数据数组:
public function someAction()
{
//...
return array('result'=>$data);
}
但我不能将这种方法用于 Json。我应该在 dispatch 事件中做什么来将结果包装在 JsonModel 中(对于适当的 accept header)?
您必须在 module.config.php 下的视图管理器中添加一个 ViewJsonStrategy 策略:
'view_manager' => array(
'template_map' => array(
),
'template_path_stack' => array(
__DIR__ . '/../view',
),
'strategies' => array(
'ViewJsonStrategy',
),
),
然后您可以 return 在您的操作中使用 JsonModel:
public function myAction()
{
$data = [1, 2, 3, 4];
return new JsonModel([
'data' => $data
]);
}
要从控制器获取 json 数据,您可以回显 json 编码数据并退出。我将其用于 jquery ajax。我希望这就是您要找的。
public function testAction()
{
$active = "some data";
echo json_encode(array('result' => $active));
exit();
}
然后在 jquery 你可以得到这样的数据
$.ajax({
type: 'GET',
url: '/index/time',
dataType: 'json',
error: function() {
$('#info').html('<p>Error on time calculation</p>');
},
success: function(data) {
data.result
}
});
真的很简单
添加如下:
IndexController.php
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Zend\View\Model\JsonModel; // Add this line
class IndexController extends AbstractActionController {
public function indexAction() {
// some index action
return new ViewModel();
}
public function apiAction() {
$person = array(
'first_name' => 'John',
'last_name' => 'Downe',
);
// Special attention to the next line
return new JsonModel(array(
'data' => $person,
));
}
}
api.phtml
<?php echo $this->json($this->data); ?>
结果:
{"first_name":"John","last_name":"Downe"}
只需为所有 API 控制器创建 Base Controller,并替换 MvcEvent 中的模型。
class JsonRestController extends AbstractRestfulController
{
public function onDispatch(MvcEvent $e)
{
$e->setViewModel(new JsonModel());
return parent::onDispatch($e);
}
}
默认 ViewModel 不是强制性的,我可以 return 从控制器中获取数据数组:
public function someAction()
{
//...
return array('result'=>$data);
}
但我不能将这种方法用于 Json。我应该在 dispatch 事件中做什么来将结果包装在 JsonModel 中(对于适当的 accept header)?
您必须在 module.config.php 下的视图管理器中添加一个 ViewJsonStrategy 策略:
'view_manager' => array(
'template_map' => array(
),
'template_path_stack' => array(
__DIR__ . '/../view',
),
'strategies' => array(
'ViewJsonStrategy',
),
),
然后您可以 return 在您的操作中使用 JsonModel:
public function myAction()
{
$data = [1, 2, 3, 4];
return new JsonModel([
'data' => $data
]);
}
要从控制器获取 json 数据,您可以回显 json 编码数据并退出。我将其用于 jquery ajax。我希望这就是您要找的。
public function testAction()
{
$active = "some data";
echo json_encode(array('result' => $active));
exit();
}
然后在 jquery 你可以得到这样的数据
$.ajax({
type: 'GET',
url: '/index/time',
dataType: 'json',
error: function() {
$('#info').html('<p>Error on time calculation</p>');
},
success: function(data) {
data.result
}
});
真的很简单
添加如下:
IndexController.php
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Zend\View\Model\JsonModel; // Add this line
class IndexController extends AbstractActionController {
public function indexAction() {
// some index action
return new ViewModel();
}
public function apiAction() {
$person = array(
'first_name' => 'John',
'last_name' => 'Downe',
);
// Special attention to the next line
return new JsonModel(array(
'data' => $person,
));
}
}
api.phtml
<?php echo $this->json($this->data); ?>
结果:
{"first_name":"John","last_name":"Downe"}
只需为所有 API 控制器创建 Base Controller,并替换 MvcEvent 中的模型。
class JsonRestController extends AbstractRestfulController
{
public function onDispatch(MvcEvent $e)
{
$e->setViewModel(new JsonModel());
return parent::onDispatch($e);
}
}