как показано ниже в ~/bootstrap/app.php
Код: Выделить всё
->withExceptions(function (Exceptions $exceptions) {
$exceptions->respond(function (Response $response, $e, Request $request) {
if ($e instanceof ValidationException) {
return response()->json($e->errors(), Response::HTTP_UNPROCESSABLE_ENTITY);
} elseif ($e instanceof MethodNotAllowedHttpException) {
$result['errorMsg'] = 'Method not allowed.';
$result['body'] = ['requestedUrl' => $request->getUri()];
return response()->json($result, Response::HTTP_METHOD_NOT_ALLOWED);
} elseif ($e instanceof ModelNotFoundException || $e instanceof NotFoundHttpException || $e instanceof RouteNotFoundException) {
$result['errorMsg'] = $e->getMessage() ?? 'Record not found.';
$result['body'] = ['requestedUrl' => $request->getUri()];
return response()->json($result, Response::HTTP_NOT_FOUND);
} elseif ($e instanceof AuthenticationException) {
$result['errorMsg'] = 'Unauthenticated';
$result['body'] = ['requestedUrl' => $request->getUri()];
return response()->json($result, Response::HTTP_UNAUTHORIZED);
} elseif ($e instanceof HttpException) {
return response()->json($e->getMessage(), $e->getStatusCode());
} else {
$result['errorMsg'] = 'Internal server error.';
$result['body'] = ['requestedUrl' => $request->getUri()];
$result['exception'] = $e->getMessage();
return response()->json($result, Response::HTTP_INTERNAL_SERVER_ERROR);
}
return $response;
});
Код: Выделить всё
public function renderable(callable $renderUsing)
{
Подробнее здесь: https://stackoverflow.com/questions/790 ... n-for-apis