tyler36/response-macro

此包的最新版本(1.1)没有可用的许可证信息。

标准化JSON响应

安装: 20

依赖: 0

建议者: 0

安全: 0

星标: 0

关注者: 1

分支: 0

开放问题: 0

类型:laravel-plugin

1.1 2018-07-20 06:03 UTC

This package is not auto-updated.

Last update: 2024-09-15 04:42:13 UTC


README

这些宏有助于在整个应用程序中标准化 api json响应 返回。灵感来源于 Laravel Response Macros for APIs

安装

  • 安装包
composer require tyler36/response-macro

成功

返回带有可选的 HTTP状态码($statusCode)的数据负载($data)[默认:[HTTP状态码200]

response()->success($data);

示例:

response()->success(['earth' => 3, 'sun' => 'yellow'])

返回以下内容 HTTP状态200

{"errors":false,"data":{"earth":3,"sun":"yellow"}}

无内容

返回空内容,带有 HTTP状态码402

response()->noContent()

错误

返回带有可选的 HTTP状态码($statusCode)的消息($message)内容[默认:[HTTP状态码400]

response()->error($message);

示例:

response()->error('There was an error.');

返回以下内容 HTTP状态400

{"errors":true,"message":"There was an error."}

示例:

response()->error('Not authorized!', 403);

返回以下内容 HTTP状态403

{"errors":true,"message":"Not authorized!"}