Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Laravel始终返回json中验证错误的错误包_Laravel_Laravel 5_Laravel 5.4 - Fatal编程技术网

Laravel始终返回json中验证错误的错误包

Laravel始终返回json中验证错误的错误包,laravel,laravel-5,laravel-5.4,Laravel,Laravel 5,Laravel 5.4,我正在开发一个api,它还应该提供有关验证问题的消息 当硬编码验证器时,我正在做这样的事情 if ($validator->fails()) { return response()->json($validator->errors(), 400); } 这很好用-但是我想要一个通用的解决方案,基本上捕获所有ValidationException,并执行与上面相同的操作 我已经尝试过使用Handler.php的render函数 然而,我找不到一种正确的方法来返回我想要的

我正在开发一个api,它还应该提供有关验证问题的消息

当硬编码验证器时,我正在做这样的事情

if ($validator->fails()) {
    return response()->json($validator->errors(), 400);
}
这很好用-但是我想要一个通用的解决方案,基本上捕获所有ValidationException,并执行与上面相同的操作

我已经尝试过使用Handler.php的render函数


然而,我找不到一种正确的方法来返回我想要的实际相关数据,这有点愚蠢——实际上拉威尔已经提供了我想要的。处理程序扩展ExceptionHandler,它执行以下操作:

public function render($request, Exception $e)
{
    $e = $this->prepareException($e);

    if ($e instanceof HttpResponseException) {
        return $e->getResponse();
    } elseif ($e instanceof AuthenticationException) {
        return $this->unauthenticated($request, $e);
    } elseif ($e instanceof ValidationException) {
        return $this->convertValidationExceptionToResponse($e, $request);
    }

    return $this->prepareResponse($request, $e);
}
和convertValidationExceptionToResponse:

    if ($e->response) {
        return $e->response;
    }

    $errors = $e->validator->errors()->getMessages();

    if ($request->expectsJson()) {
        return response()->json($errors, 422);
    }

    return redirect()->back()->withInput(
        $request->input()
    )->withErrors($errors);

因此,这正是我想要的

如果您总是希望它返回json,您可以按照文档中的方式进行验证:$this->validate$request、$rules、$messages,并覆盖light\Foundation\validation\validateRequests trait中的buildFailedValidationResponse方法,以便它始终生成json响应。请不要只是发布代码。相反,请解释为什么您的代码可以解决ops问题。
public function render($request, Exception $exception)
{
 if ($request->wantsJson() && (str_contains('api/v1/register', $request->path())) || (str_contains('api/v1/login', $request->path())) ) {
$errors = null ;
if($exception->validator){
$errors = $exception->validator->errors();
}
return response()->json([
'status' => 422,'success'=> false,'message'=>''. $exception->getMessage() ,'error'=> "" . $errors
            ], 422);
}
return parent::render($request, $exception);
}
public function render($request, Exception $exception)
{
 if ($request->wantsJson() && (str_contains('api/v1/register', $request->path())) || (str_contains('api/v1/login', $request->path())) ) {
$errors = null ;
if($exception->validator){
$errors = $exception->validator->errors();
}
return response()->json([
'status' => 422,'success'=> false,'message'=>''. $exception->getMessage() ,'error'=> "" . $errors
            ], 422);
}
return parent::render($request, $exception);
}