如何在 restful Zend framework 2 实现中获取 update($id, $data) 中的 $data
How to get the $data in update($id, $data) in restful implementation of Zend framework 2
Zend framework 2restful实现中如何获取update($id, $data)中的$data
在 Postman 中,我正在尝试发送..
{
"gender":"1",
"country":"1",
"state":"2",
"city":"23",
"address1":"27,djkfhasdkjh,kjsdfhdkjs,kjsdh",
"address2":"ksjadh f,sdkjfhjk",
"postal_code":"627811",
"birthdate":"11-12-2045",
"phone":"0442805565",
"mobile":"9865521557",
"blood_group":"o-",
"weight":"60",
"height":"5.2",
"bmi":"10",
}
我的休息密码是:
//更新::更新
public函数更新($id,$data){
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST');
header("Access-Control-Allow-Headers: X-Requested-With, Content-Type");
$data = json_decode($data);
print_r($data);
if(empty($data)){
$resp = array('status' => 'failure', 'errorCode' => 516, 'errorMessage' => 'json code format error');
return new JsonModel($resp);
}
if ((strlen($data->mobile) >= 10) && (is_numeric($data->mobile))) {
}
else{
$resp = array('status' => 'failure', 'errorCode' => 517, 'errorMessage' => 'mobile validation error');
return new JsonModel($resp);
}
if(($data->gender == '1') || ($data->gender == '2')){
}
else{
$resp = array('status' => 'failure', 'errorCode' => 517, 'eerorMessage' => 'gender validation error');
return new JsonModel($resp);
}
$id = $this->params('id');
$sm = $this->getServiceLocator();
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$usersService = new UsersService($dbAdapter);
$resp = $usersService->update($data,$id);
$resp = array('status' => 'success');
return new JsonModel($resp);
exit;
谢谢,
class MyController extends AbstractRestfulController
{
public function update($id, $data)
{
var_dump($id, $data);
}
}
发送JSON到url .../mycontroller/1
{"data":"15"}
方法 PUT
内容类型:application/json
output:
array(1, 'data' => 15);
在您的情况下(PUT
请求映射到 update
方法)AbstractRestfulController
将从 processBodyContent
method on line 419
获取数据
此方法将使用 requestHasContentType
方法检查正确的内容类型 (application/json
)。因此,如果您将请求中的 Content-Type
header 设置为 application/json
,控制器中的 $data
变量应该会自动填充。
所以在你的邮递员中确保你有 Content-Type
header 设置:
Note: You don't have to decode json data yourself, this is handled in the controller.
Zend framework 2restful实现中如何获取update($id, $data)中的$data
在 Postman 中,我正在尝试发送..
{
"gender":"1",
"country":"1",
"state":"2",
"city":"23",
"address1":"27,djkfhasdkjh,kjsdfhdkjs,kjsdh",
"address2":"ksjadh f,sdkjfhjk",
"postal_code":"627811",
"birthdate":"11-12-2045",
"phone":"0442805565",
"mobile":"9865521557",
"blood_group":"o-",
"weight":"60",
"height":"5.2",
"bmi":"10",
}
我的休息密码是:
//更新::更新 public函数更新($id,$data){
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST');
header("Access-Control-Allow-Headers: X-Requested-With, Content-Type");
$data = json_decode($data);
print_r($data);
if(empty($data)){
$resp = array('status' => 'failure', 'errorCode' => 516, 'errorMessage' => 'json code format error');
return new JsonModel($resp);
}
if ((strlen($data->mobile) >= 10) && (is_numeric($data->mobile))) {
}
else{
$resp = array('status' => 'failure', 'errorCode' => 517, 'errorMessage' => 'mobile validation error');
return new JsonModel($resp);
}
if(($data->gender == '1') || ($data->gender == '2')){
}
else{
$resp = array('status' => 'failure', 'errorCode' => 517, 'eerorMessage' => 'gender validation error');
return new JsonModel($resp);
}
$id = $this->params('id');
$sm = $this->getServiceLocator();
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$usersService = new UsersService($dbAdapter);
$resp = $usersService->update($data,$id);
$resp = array('status' => 'success');
return new JsonModel($resp);
exit;
谢谢,
class MyController extends AbstractRestfulController
{
public function update($id, $data)
{
var_dump($id, $data);
}
}
发送JSON到url .../mycontroller/1
{"data":"15"}
方法 PUT
内容类型:application/json
output:
array(1, 'data' => 15);
在您的情况下(PUT
请求映射到 update
方法)AbstractRestfulController
将从 processBodyContent
method on line 419
此方法将使用 requestHasContentType
方法检查正确的内容类型 (application/json
)。因此,如果您将请求中的 Content-Type
header 设置为 application/json
,控制器中的 $data
变量应该会自动填充。
所以在你的邮递员中确保你有 Content-Type
header 设置:
Note: You don't have to decode json data yourself, this is handled in the controller.