【问题标题】:FOSRestBundle return object errorFOSRestBundle 返回对象错误
【发布时间】:2016-01-18 14:22:25
【问题描述】:

我使用 FOERestBundle 和类视图。当我验证实体时,我有这样的对象错误,这是:

[
 {
   "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."
 }
]

如何返回自定义错误,如对象而不是纯文本?

【问题讨论】:

    标签: php validation symfony fosrestbundle


    【解决方案1】:

    只需像数组一样传递您的数据并告诉视图将其渲染为 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);
    

    【讨论】:

      【解决方案2】:

      您可以使用 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

      【讨论】:

      • 我已经把逻辑。我在 prod twig 中有: exception_controller: ArtelProfileBundle:Exception:showException 这个动作 = return $this->redirect($this->generateUrl('login_route'));而BadRequestHttpException,NotFoundHttpException 不会在prod中返回prod中返回login_page,但是很好地查看prod中的工作
      猜你喜欢
      • 1970-01-01
      • 2014-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-19
      • 2011-06-10
      • 2014-07-27
      • 1970-01-01
      相关资源
      最近更新 更多