[英]FOSRestBundle return object error
我使用FOERestBundle和類View。 當我驗證實體時,我遇到了這樣的對象錯誤,這是:
[
{
"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."
}
]
當用戶無效的安全令牌時,我需要返回對象錯誤
[
{
"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"
這是返回對象錯誤驗證,沒關系
[
{
"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."
}
]
如何返回自定義錯誤,例如對象不是純文本?
只需像數組一樣傳遞您的數據,並告訴視圖將其呈現為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
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.