Package Data | |
---|---|
Maintainer Username: | caikeal |
Maintainer Contact: | 546487590@qq.com (caikeal) |
Package Create Date: | 2017-02-15 |
Package Last Update: | 2017-03-10 |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-11-12 15:03:50 |
Package Statistics | |
---|---|
Total Downloads: | 39 |
Monthly Downloads: | 0 |
Daily Downloads: | 0 |
Total Stars: | 0 |
Total Watchers: | 2 |
Total Forks: | 0 |
Total Open Issues: | 0 |
Laravel
项目规范Api
输出和错误提示(测试版本)建议使用Laravel >= 5.1.* , PHP >= 5.6
composer require keal/laravel-output
在app.php
中加入Caikeal\Output\Provider\OutputServiceProvider::class
需要在Exceptions/Handler.php
中引入Caikeal\Output\Exceptions\HandlerTrait
该方法,
然后在改写下render
方法,如下:
public function render($request, Exception $exception)
{
if ($request->expectsJson()) {
return $this->handlerApi($exception);
}
return parent::render($request, $exception);
}
在所有Controller
的基类中引入该方法,如下:
protected function response()
{
return app('output.response');
}
在返回时可以用该方法代替response返回:$this->reponse()
的方法
所有方法如下:
正确值返回:
$this->response()->item([Model], [Transformer]);
$this->response()->collection([Model], [Transformer]);
$this->response()->paginator([Model], [Transformer]);
$this->response()->array([Model/Array]);
返回值样式:
{
"data": {
"name": "keal"
},
"meta": {
"type": "seller"
}
}
错误值返回:
$this->response()->errorNotFound([$message], [$code])
$this->response()->errorBadRequest([$message], [$code])
$this->response()->errorForbidden([$message], [$code])
$this->response()->errorInternal([$message], [$code])
$this->response()->errorUnauthorized([$message], [$code])
$this->response()->errorMethodNotAllowed([$message], [$code])
$this->response()->error([$message], [$statusCode], [$code])
返回值样式:
{
"code": "401000",
"status_code": "401",
"message": "Wrong."
}
Transformer
的数据格式转换在所有Transformer
类中必须继承League\Fractal\TransformerAbstract
,使用如下:
namespace App\Transformers;
use App\User;
use League\Fractal\TransformerAbstract;
class UserTransformer extends TransformerAbstract
{
public function transform(User $data){
return [
'id'=>intval($data['id']),
'name'=>ucfirst($data['name'])
];
}
}
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