urameshibr/jsonresponse

laravel 中 JSON 响应的辅助工具

1.1 2017-07-19 21:02 UTC

This package is auto-updated.

Last update: 2024-09-18 18:03:19 UTC


README

laravel 项目中 JSON 响应的简单辅助工具

如何安装

composer require urameshibr/jsonresponse

如何使用

该辅助工具需要 4 个参数

  • 状态

  • HTTP 状态码

  • 消息

  • 数据(可选)

示例

<?php
// Any controller
public function show($id)
{
  $customer = $this->repository->showCustomerData($id);
  
  return $customer-isEmpty()
    ? json_response(false, 404, "Customer not found")
    : json_response(true, 200, "Customer info", $customer);
}

这将返回一个 JSON

{
  "status": "true",
  "code": 200,
  "message": "Customer info",
  "data": {
  // customer data
  }
}

或者

{
  "status": "false",
  "code": 404,
  "message": "Customer not found.",
  "data": null
}
  • 注意:* 该软件包使用 Illuminate 的 "response()" 辅助工具。

  • 注意:* 可以使用 404 表示 "数据未找到" 或 422。 (或 666)