Download the PHP package keal/laravel-output without Composer
On this page you can find all versions of the php package keal/laravel-output. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download keal/laravel-output
More information about keal/laravel-output
Files in keal/laravel-output
Package laravel-output
Short Description Laravel standard output for restful api
License MIT
Informations about the package laravel-output
关于Laravel
项目规范Api
输出和错误提示(测试版本)
安装
建议使用Laravel >= 5.1.* , PHP >= 5.6
composer require keal/laravel-output
使用
注册ServiceProvider
在app.php
中加入Caikeal\Output\Provider\OutputServiceProvider::class
引入错误处理
需要在Exceptions/Handler.php
中引入Caikeal\Output\Exceptions\HandlerTrait
该方法,
然后在改写下render
方法,如下:
引入返回值基类
在所有Controller
的基类中引入该方法,如下:
返回方法
在返回时可以用该方法代替response返回:$this->reponse()
的方法
所有方法如下:
-
正确值返回:
- model层,且带有transformer验证的单列数据:
$this->response()->item([Model], [Transformer]);
- model层,且带有transformer验证的多列数据:
$this->response()->collection([Model], [Transformer]);
- model层,且带有transformer验证的分页数据:
$this->response()->paginator([Model], [Transformer]);
- 任意:
$this->response()->array([Model/Array]);
返回值样式:
- model层,且带有transformer验证的单列数据:
-
错误值返回:
- 404错误:
$this->response()->errorNotFound([$message], [$code])
- 400错误:
$this->response()->errorBadRequest([$message], [$code])
- 403错误:
$this->response()->errorForbidden([$message], [$code])
- 500错误:
$this->response()->errorInternal([$message], [$code])
- 401错误:
$this->response()->errorUnauthorized([$message], [$code])
- 405错误:
$this->response()->errorMethodNotAllowed([$message], [$code])
- 任意错误:
$this->response()->error([$message], [$statusCode], [$code])
返回值样式:
- 404错误:
Transformer
的数据格式转换
在所有Transformer
类中必须继承League\Fractal\TransformerAbstract
,使用如下:
Artsian
创建Transformer
类
创建Transformer:php artisan make:transformer User
附带Model名:php artisan make:transformer User -m User
或者php artisan make:transformer User --model=User
或者php artisan make:transformer User --model=Model/User
带位置,命令会自动转化成空间名,并在开头添加App
感谢
主要借鉴和引用了Dingo/api和league/fractal