dept-eduk/json-response

该软件包最新版本(3.0.0)没有可用的许可信息。

3.0.0 2019-10-31 10:04 UTC

This package is not auto-updated.

Last update: 2024-09-28 07:46:46 UTC


README

该软件包提供了一种响应包装器,为JSON API添加了功能。

使用Composer引入

  1. 安装软件包: composer require e3creative/json-response

用法

您可以使用Laravel的Response类,并结合此软件包提供的附加JSON方法,例如。

Response::empty();
Response::created($data);
Response::success($message, $status);
Response::error($message, $status, $data);
Response::unprocessable($errors);
public function index(Response $response) {
    return $response->empty();
}

如果您遵循上述安装说明中的第二步,您还可以使用这些方法与response()函数一起使用

json_response()->empty();
json_response()->created($data);
json_response()->success($message, $status);
json_response()->error($message, $status, $data);
json_response()->unprocessable($errors);

错误响应代码

error()方法期望一个四位数响应代码。这将转换为三位数HTTP响应代码,但四位数的响应代码将返回在响应体中。这允许在app/frontends中进行更细致的错误响应标识。