satheez/api-response
最新稳定版本:v1.1
Composer 安装命令:
composer require satheez/api-response
包简介
Standard API response class
README 文档
README
Standard API response class
This is where your description should go. Limit it to a paragraph or two. Consider adding a small example.
Installation
You can install the package via composer:
composer require satheez/api-response
You can publish the config file with:
php artisan vendor:publish --tag="api-response-config"
Usage
Example
<?php namespace App\Http\Controllers\Api; use App\Models\User; use Illuminate\Http\JsonResponse; use App\Http\Controllers\Controller; use Illuminate\Http\Request; class UserController extends Controller { /** * Display a listing of the resource. * * @return JsonResponse */ public function index() { // todo, paginate the user data return api()->success(User::all()->toArray()); } /** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return JsonResponse */ public function store(Request $request): JsonResponse { // todo validate the request data $user = User::create($request->all()); return api()->created($user->toArray()); } /** * Display the specified resource. * * @param int $id * @return JsonResponse */ public function show($id): JsonResponse { $user = User::find($id); // todo update process return !empty($user) ? api()->success($user->toArray()) : api()->notFound(); } /** * Update the specified resource in storage. * * @param \Illuminate\Http\Request $request * @param int $id * @return JsonResponse */ public function update(Request $request, $id): JsonResponse { // todo update process return api()->stored(User::find($id)->toArray()); } /** * Remove the specified resource from storage. * * @param int $id * @return JsonResponse */ public function destroy($id): JsonResponse { User::destroy($id); return api()->deleted(); } }
Available methods
Success Methods ✌️
| Method | Status code | Description |
|---|---|---|
| api()->success() | 200 | Successful get, patch (return a JSON object) |
| api()->created() | 201 | Successful record create (return a JSON object) |
| api()->updated() | 200 | Successful record update (return a JSON object) |
Error Status 💩
| Method | Status code | Description |
|---|---|---|
| api()->unauthorized() | 401 | Error Not authenticated |
| api()->invalidRequest() | 400 | Error invalid request |
| api()->accessDenied() | 403 | Error Not authorized (Authenticated, but no permissions) |
| api()->forbidden() | 403 | Error Not authorized (Authenticated, but no permissions) |
| api()->notFound() | 404 | Error Not Found |
| api()->validationError($message) | 422 | Error Validation |
| api()->somethingWentWrong() | 500 | Internal error |
| api()->exception($exception) | Error with exception |
Extra methods 👨
| Method | Status code | Description |
|---|---|---|
| api()->error($message, $httpCode) | 422 (default) | Custom Error response |
| api()->success($data, $message, $httpCode) | 200 (default) | Custom Success response |
Testing
composer test
Credits
License
The MIT License (MIT). Please see License File for more information.
统计信息
- 总下载量: 3
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 4
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-07-23