isaackearl / artisan-api
An api service for Laravel or Lumen. Helps you send responses with the proper status and code. Uses Fractal for items and collections.
Requires
- php: ~5.6|~7.0
- doctrine/inflector: ~1.1.0
- doctrine/instantiator: ~1.0.5
- illuminate/http: ~5.1
- illuminate/pagination: ~5.1
- illuminate/routing: ~5.1
- illuminate/support: ~5.1
- spatie/laravel-fractal: ^4.3
Requires (Dev)
- mockery/mockery: ^0.9.9
- phpunit/php-token-stream: ~1.4.11
- phpunit/phpunit: ~4.0||~5.0||~6.0
- satooshi/php-coveralls: ^1.0
This package is not auto-updated.
Last update: 2020-08-22 05:39:24 UTC
README
An api service for Laravel or Lumen. Helps you send responses with the proper status and code. Uses Fractal for items and collections.
Setup/Install
Require it with Composer
$ composer require isaackearl/artisan-api
Add the service provider to config/app.php
// For Laravel add this to config/app.php IsaacKenEarl\LaravelApi\Providers\ArtisanApiServiceProvider::class // For Lumen add this to bootstrap/app.php $app->register(IsaacKenEarl\LaravelApi\Providers\ArtisanApiServiceProvider::class);
(Optional) Add the API facade in config/app.php
'Api' => IsaacKenEarl\LaravelApi\Facades\Api::class,
Usage
In your controllers you can do stuff like this:
// do stuff like this public function show() { return Api::respondWithItem($user, new UserTransformer()); } // or like this: return Api::respondNotFound();
There are alot of options. Include the ArtisanApiInterface in your controller constructor and you can use it without the facade.
private $api; public function __construct(ArtisanApiServiceInterface $apiService) { $this->api = $apiService; } public function index() { $users = User::all(); return $this->api->respondWithCollection($users, new UserTransformer()); }
You can do custom stuff too and chain methods
// you can respondWithError or respondWithMessage and customize the status code // and response code etc return $this->api ->setStatus(401) ->setResponseCode(ResponseCodes::UNAUTHORIZED) ->respondWithError('Not logged in');
Take a look at the ArtisanApiInterface to see all the supported methods. You can find that here:
Transformers
Transformers allow you to control how the data is presented in the response of your API. A typical transformer looks like this:
class UserTransformer extends Transformer { function transform($user) { return [ 'id' => $user->id, 'name' => $user->name, 'date_of_birth' => $user->date_of_birth->toDateString(), 'email' => $user->getPrimaryEmail() ]; } }
You can generate a transformer with the make:transformer command
php artisan make:transformer UserTransformer
This package uses laravel-fractal as it's fractal implementation. Check out their docs on their github page for more specific usage information and examples.
Since we are using the laravel-fractal package you can also publish the laravel-fractal config to customize the response data.
php artisan vendor:publish --provider="Spatie\Fractal\FractalServiceProvider"
Testing
$ composer test
Contributing
Please see CONTRIBUTING and CONDUCT for details.
Credits
License
The MIT License (MIT). Please see License File for more information.