firebit/laravel-api-response

一个简单的Laravel包,用于创建符合JSend和HTTP标准的API响应。

0.1.2.2 2020-10-30 09:59 UTC

This package is not auto-updated.

Last update: 2024-09-28 04:20:36 UTC


README

Firebit logo

Laravel API Response

此包旨在使API响应一致且易于使用。它符合HTTP标准和JSend标准。

示例

代码

function getUser($id){
    // Get the data we wish to return
    $user = User:find($id);
    
    // If the user does not exist we return an error
    if(!$user){
        return ApiResponseFactory::notFound("User does not exist");
    }
    
    // Return the data by using a ReponseFactory
    return ApiResponseFactory::success($user);
}

错误输出

{
  "status":"fail",
  "message":"User does not exist"
}

成功输出

{
  "status":"success",
  "data": { 
      "id":1,
      "name":"John Doe",
      "email":"john@example.org",
      "created_at":null,
      "updated_at":null
  }
}

安装

要安装,可以使用Composer,使用以下命令安装此包。
composer require firebit/laravel-api-response

文档

即将推出!

进度

  • JSend兼容性
  • PHPUnit测试
  • 文档

许可证

有关许可证,请参阅LICENSE文件,本项目采用MIT许可证。