2017 © Pedro Peláez
 

library laravel-api

A laravel api develop package

image

tanmo/laravel-api

A laravel api develop package

  • Saturday, April 28, 2018
  • by bever
  • Repository
  • 1 Watchers
  • 0 Stars
  • 33 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 4 Versions
  • 313 % Grown

The README.md

tanmo/laravel-api

模仿 dingo/api 的响应方式,基于Laravel Api resource 做转换层的api开发扩展包, (*1)

依赖

Laravel 5.5以上版本, (*2)

安装

composer require tanmo/laravel-api

发布资源, (*3)

php artisan vendor:publish --provider="Tanmo\Api\Providers\ApiServiceProvider"

使用

可以在控制器中引入 Helpers Trait ,也可以使用 api() 辅助函数, (*4)

<?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);
    }
}

响应item

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);

设置Meta元素

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 才会显示调试信息, (*5)

api()->errorForbidden();
api()->errorNotFound();
api()->errorBadRequest();
api()->errorInternal();
api()->errorUnauthorized();
api()->errorMethodNotAllowed();

The Versions

28/04 2018

dev-master

9999999-dev

A laravel api develop package

  Sources   Download

MIT

by Tanmo

28/04 2018

v1.0.2

1.0.2.0

A laravel api develop package

  Sources   Download

MIT

by Tanmo

27/04 2018

v1.0.1

1.0.1.0

A laravel api develop package

  Sources   Download

MIT

by Tanmo

11/04 2018

v1.0.0

1.0.0.0

A laravel api develop package

  Sources   Download

MIT

by Tanmo