e3creative / json-response
2.0.0
2019-06-19 14:23 UTC
Requires (Dev)
- orchestra/testbench: ^3.5
- phpunit/phpunit: ^8.2
This package is not auto-updated.
Last update: 2019-10-31 10:26:52 UTC
README
此包提供了一个响应包装器,为json api添加了功能。
使用composer拉取
- 安装此包:
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中提供更精细的错误响应识别。