FOSRestBundle return 对象错误
FOSRestBundle return object error
我使用 FOERestBundle 和 class 视图。当我验证实体时,我有这样的对象错误,这是:
[
{
"property_path": "main_skill",
"message": "This value should not be blank."
},
{
"property_path": "type",
"message": "This value should not be blank."
},
{
"property_path": "description",
"message": "This value should not be blank."
}
]
当用户不是这样的有效安全令牌时,我需要 return 对象错误
[
{
"property_path": "main_skill",
"message": "This value should not be blank."
},
]
现在我有了纯文本。这是我的终点
/**
* Update existing Bit from the submitted data.
*
* @ApiDoc(
* resource = true,
* description = "Update single Bit",
* parameters={
* {"name"="status", "dataType"="string", "required"=false, "description"="status for bit"},
* {"name"="text", "dataType"="string", "required"=true, "description"="text for rejected"},
* {"name"="token", "dataType"="string", "required"=true, "description"="is equally md5('email'.secret_word)"}
* },
* statusCodes = {
* 200 = "Bit successful update",
* 400 = "Secret token is not valid"
* },
* section="Bit"
* )
* @RestView()
*
* @param Request $request
* @param string $id
*
* @return View
*/
public function putBitAction(Request $request, $id)
{
$manager = $this->getDoctrine()->getManager();
$token = $this->get('request')->request->get('token');
$user = $this->getDoctrine()->getRepository('MyBundle:Users')->findOneBySecuritytoken($token);
$bit = $manager->getRepository('MyBundle:Bit')->find($id);
$view = View::create();
if (!empty($user) && !empty($bit) && !empty($token)) {
*some logic
$view = $this->view($bit, 200);
return $this->handleView($view);
}
} else {
$view = $this->view('Secret token is not valid', 400);
return $this->handleView($view);
}
}
现在我有了纯文本
Response Body [Raw]
"Secret token is not valid"
这是 return 对象错误验证,这没问题
[
{
"property_path": "main_skill",
"message": "This value should not be blank."
},
{
"property_path": "type",
"message": "This value should not be blank."
},
{
"property_path": "description",
"message": "This value should not be blank."
}
]
如何return自定义错误,比如对象不是纯文本?
只需像数组一样传递您的数据,并告诉视图将其呈现为 json 应该会生成您想要的输出
$view = $this->view(
array(
'property_path' => 'main_skill',
'message' => "error"
//whatever your object/array structure is
),
500 //error code for the error
);
$view->setFormat('json');
return $this->handleView($view);
您可以使用 Symfony 的 HTTPExceptions,因为它们将由 FOSRestBundle 处理。
参见:http://symfony.com/doc/current/bundles/FOSRestBundle/4-exception-controller-support.html
public function putBitAction(Request $request, $id)
{
$token = $request->get('token');
if (null === $token) {
throw new BadRequestHttpException('Provide a secret token');
}
$manager = $this->getDoctrine()->getManager();
$user = $manager->getRepository('MyBundle:Users')->findOneBySecuritytoken($token);
if (null === $user) {
throw new BadRequestHttpException('Secret token is not valid');
}
$bit = $manager->getRepository('MyBundle:Bit')->find($id);
if (null === $token) {
throw new NotFoundHttpException('Bid not found');
}
$view = $this->view($bit, 200);
return $this->handleView($view);
}
这是一个怎样的 PUT
请求?您应该重命名为 getBidAction
.
我使用 FOERestBundle 和 class 视图。当我验证实体时,我有这样的对象错误,这是:
[
{
"property_path": "main_skill",
"message": "This value should not be blank."
},
{
"property_path": "type",
"message": "This value should not be blank."
},
{
"property_path": "description",
"message": "This value should not be blank."
}
]
当用户不是这样的有效安全令牌时,我需要 return 对象错误
[
{
"property_path": "main_skill",
"message": "This value should not be blank."
},
]
现在我有了纯文本。这是我的终点
/**
* Update existing Bit from the submitted data.
*
* @ApiDoc(
* resource = true,
* description = "Update single Bit",
* parameters={
* {"name"="status", "dataType"="string", "required"=false, "description"="status for bit"},
* {"name"="text", "dataType"="string", "required"=true, "description"="text for rejected"},
* {"name"="token", "dataType"="string", "required"=true, "description"="is equally md5('email'.secret_word)"}
* },
* statusCodes = {
* 200 = "Bit successful update",
* 400 = "Secret token is not valid"
* },
* section="Bit"
* )
* @RestView()
*
* @param Request $request
* @param string $id
*
* @return View
*/
public function putBitAction(Request $request, $id)
{
$manager = $this->getDoctrine()->getManager();
$token = $this->get('request')->request->get('token');
$user = $this->getDoctrine()->getRepository('MyBundle:Users')->findOneBySecuritytoken($token);
$bit = $manager->getRepository('MyBundle:Bit')->find($id);
$view = View::create();
if (!empty($user) && !empty($bit) && !empty($token)) {
*some logic
$view = $this->view($bit, 200);
return $this->handleView($view);
}
} else {
$view = $this->view('Secret token is not valid', 400);
return $this->handleView($view);
}
}
现在我有了纯文本
Response Body [Raw]
"Secret token is not valid"
这是 return 对象错误验证,这没问题
[
{
"property_path": "main_skill",
"message": "This value should not be blank."
},
{
"property_path": "type",
"message": "This value should not be blank."
},
{
"property_path": "description",
"message": "This value should not be blank."
}
]
如何return自定义错误,比如对象不是纯文本?
只需像数组一样传递您的数据,并告诉视图将其呈现为 json 应该会生成您想要的输出
$view = $this->view(
array(
'property_path' => 'main_skill',
'message' => "error"
//whatever your object/array structure is
),
500 //error code for the error
);
$view->setFormat('json');
return $this->handleView($view);
您可以使用 Symfony 的 HTTPExceptions,因为它们将由 FOSRestBundle 处理。
参见:http://symfony.com/doc/current/bundles/FOSRestBundle/4-exception-controller-support.html
public function putBitAction(Request $request, $id)
{
$token = $request->get('token');
if (null === $token) {
throw new BadRequestHttpException('Provide a secret token');
}
$manager = $this->getDoctrine()->getManager();
$user = $manager->getRepository('MyBundle:Users')->findOneBySecuritytoken($token);
if (null === $user) {
throw new BadRequestHttpException('Secret token is not valid');
}
$bit = $manager->getRepository('MyBundle:Bit')->find($id);
if (null === $token) {
throw new NotFoundHttpException('Bid not found');
}
$view = $this->view($bit, 200);
return $this->handleView($view);
}
这是一个怎样的 PUT
请求?您应该重命名为 getBidAction
.