2017 © Pedro Peláez
 

library 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.

image

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.

  • Sunday, June 17, 2018
  • by isaackearl
  • Repository
  • 1 Watchers
  • 3 Stars
  • 31 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 6 Versions
  • 35 % Grown

The README.md

Artisan API

Latest Stable Version ![Latest Unstable Version][ico-poser-unstable] ![Software License][ico-license] Build Status [![Coverage Status][ico-coveralls]][link-coveralls] Total Downloads, (*1)

An api service for Laravel or Lumen. Helps you send responses with the proper status and code. Uses Fractal for items and collections., (*2)

Setup/Install

Require it with Composer, (*3)

``` bash $ composer require isaackearl/artisan-api, (*4)


Add the service provider to config/app.php ```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, (*5)

'Api' => IsaacKenEarl\LaravelApi\Facades\Api::class,

Usage

In your controllers you can do stuff like this:, (*6)

``` php // do stuff like this public function show() { return Api::respondWithItem($user, new UserTransformer()); }, (*7)

// or like this:, (*8)

return Api::respondNotFound();, (*9)


There are alot of options. Include the ArtisanApiInterface in your controller constructor and you can use it without the facade. ```php 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, (*10)

// 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:, (*11)

ArtisanApiInterface, (*12)

Transformers

Transformers allow you to control how the data is presented in the response of your API. A typical transformer looks like this:, (*13)

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, (*14)

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., (*15)

Since we are using the laravel-fractal package you can also publish the laravel-fractal config to customize the response data., (*16)

php artisan vendor:publish --provider="Spatie\Fractal\FractalServiceProvider"

Testing

bash $ composer test, (*17)

Contributing

Please see CONTRIBUTING and CONDUCT for details., (*18)

Credits

License

The MIT License (MIT). Please see License File for more information., (*19)

The Versions

17/06 2018

dev-dev

dev-dev https://github.com/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.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel api lumen artisan laravel api isaackenearl artisan api isaackearl

09/08 2017

dev-master

9999999-dev https://github.com/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.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel api lumen artisan laravel api isaackenearl artisan api isaackearl

09/08 2017

v1.0.2

1.0.2.0 https://github.com/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.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel api lumen artisan laravel api isaackenearl artisan api isaackearl

09/08 2017

v1.0.1

1.0.1.0 https://github.com/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.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel api lumen artisan laravel api isaackenearl artisan api isaackearl

09/08 2017

v1.0.0

1.0.0.0 https://github.com/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.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel api lumen artisan laravel api isaackenearl artisan api isaackearl

09/08 2017

v1.0.0-rc1

1.0.0.0-RC1 https://github.com/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.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel api lumen artisan laravel api isaackenearl artisan api isaackearl