模仿 dingo/api
的响应方式,基于Laravel Api resource
做转换层的api开发扩展包
Laravel 5.5以上版本
composer require tanmo/laravel-api
发布资源
php artisan vendor:publish --provider="Tanmo\Api\Providers\ApiServiceProvider"
可以在控制器中引入 Helpers Trait
,也可以使用 api()
辅助函数
<?php
use \Tanmo\Api\Traits\Helpers;
class UserController extends Controller
{
use Helpers;
public function show($id)
{
return $this->response()->item(User::find($id), UserResource::class);
}
}
return api()->item(User::find(1), UserResource::class);
return api()->collection(User::all(), UserCollection::class); //相当于 return new UserCollection(User::all());
return api()->collection(User::all(), UserResource::class); //相当于 return UserResource::collection(User::all())
// 分页,同样支持 Collection 和 Resource
return api()->collection(User::paginate(5), UserCollection::class);
return api()->item(User::find(1), UserResource::class)->setMeta(['key' => 'value'])
return api()->created();
return api()->accepted();
return api()->noContent();
需要在 config/api.php
里设置 debug = true
才会显示调试信息
api()->errorForbidden();
api()->errorNotFound();
api()->errorBadRequest();
api()->errorInternal();
api()->errorUnauthorized();
api()->errorMethodNotAllowed();