jasechen/lara-jsonponse

这个Laravel包用于通过RESTful API响应JSON消息。

0.1.0 2018-04-24 16:36 UTC

This package is not auto-updated.

Last update: 2024-09-29 05:21:10 UTC


README

这个Laravel包用于通过RESTful API响应JSON消息。

# 安装

  1. 使用composer安装此包

     $ composer require jasechen/laravel-jsonponse
    
  2. 编辑config/app.php

     $ joe config/app.php
    
     # add
     #
     'providers' => [
         ...
         Jasechen\Jsonponse\JsonponseServiceProvider::class,
         ...
     ],
    
  3. 重新加载并更新包

     $ composer dump-autoload
    

# 使用方法

app/Http/Controllers/SampleController.php中,例如

use Jasechen\Jsonponse\Jsonponse;
...

public function printSuccessMessage()
{
    Jsonponse::success('find success', ['session' => '83cec640fda431a66c293b12a4fa4a83']);
} // END function

public function printErrorMessage()
{
    Jsonponse::fail('input params error', 400);
} // END function

结果

# printSuccessMessage
#
{
    "status": "success",
    "code": 200,
    "comment": "find success",
    "data": {
        "session": "83cec640fda431a66c293b12a4fa4a83"
    }
}

# printErrorMessage
#
{
    "status": "fail",
    "code": 400,
    "comment": "input params error"
}

# 使用RESTful API的HTTP状态码

# 2xx - 成功

# 4xx - 客户端错误

# 5xx - 服务器错误

# 许可证

MIT许可证