yaroslawww/laravel-api-response

此包已被废弃,不再维护。作者建议使用 laravel/framework 包。

提交结构化响应很容易

2.0.1 2020-11-05 15:40 UTC

This package is auto-updated.

Last update: 2020-11-07 17:45:54 UTC


README

Software License Build Status StyleCI Quality Score Code Coverage PHP Version Packagist Version

安装

您可以通过 composer 安装此包

composer require yaroslawww/laravel-api-response
# .env
API_VERSION="1.1"

您可以使用以下命令发布配置文件

php artisan vendor:publish --provider="Gcsc\LaravelApiResponse\ApiResponseProvider" --tag="config"

使用方法

Route::get('/', function () {
    return ApiResponse::ok();
});
Route::get('/', function (Request $request) {
    $note = Note::create(['text' => $request->text]);
    return ApiResponse::created($note, 'New note created!');
});

版本 >= 2.0 支持 Laravel 资源对象

Route::get('/api/profile', function (Request $request) {
    return ApiResponse::created(new \App\Http\Resources\User\Profile($request->user()));
});
>>> (string)ApiResponse::setMessage('Page expired')->send([], 419);
=> """
   HTTP/1.0 419 unknown status\r\n
   Cache-Control: no-cache, private\r\n
   Content-Type:  application/json\r\n
   Date:          Wed, 29 Jan 2020 14:06:07 GMT\r\n
   \r\n
   {"data":[],"meta":{"version":"1.1","environment":"development"},"message":"Page expired"}
   """

测试

composer test

安全

如果您发现任何安全问题,请发送电子邮件至 yaroslav.georgitsa@gmail.com,而不是使用问题跟踪器。

许可

MIT 许可证 (MIT)。有关更多信息,请参阅 许可文件