2017 © Pedro PelĂĄez
 

library laravel-endpoint

Laravel Endpoint is a CRUD REST API package for Laravel.

image

sdv/laravel-endpoint

Laravel Endpoint is a CRUD REST API package for Laravel.

  • Wednesday, May 3, 2017
  • by SDV PlurimĂ©dia
  • Repository
  • 1 Watchers
  • 8 Stars
  • 3,791 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 4 Open issues
  • 12 Versions
  • 19 % Grown

The README.md

Laravel Endpoint

Latest Stable Version License Total Downloads Build Status, (*1)

Laravel Endpoint is a CRUD REST API package for Laravel., (*2)

Features

  • [X] REST CRUD Endpoint scaffolding
  • [X] Normalized JSON Response using laravel-fractal
  • [X] Simple filtering operations
  • [ ] Ability to customize fractal serializer
  • [ ] Ability to customize filtering and sorting strategies
  • [ ] Elasticsearch / Algolia search
  • [ ] Api Documentation (Swagger, API Blueprint)

Installation

You can pull in the package via composer:, (*3)

$ composer require sdv/laravel-endpoint

Register the service provider., (*4)

// config/app.php
'providers' => [
    ...
    SdV\Endpoint\EndpointServiceProvider::class,
]

Replace the render method in app/Exceptions/Handler.php., (*5)

use SdV\Endpoint\ProvidesExceptionsHandler;

class Handler extends ExceptionHandler
{
    use ProvidesExceptionsHandler;

    ...

    /**
     * Render an exception into an HTTP response.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Exception  $exception
     * @return \Illuminate\Http\Response
     */
    public function render($request, Exception $exception)
    {
        if ($this->isRequestForApi($request)) {
            return $this->renderJson($exception, $request);
        }

        return parent::render($request, $exception);
    }

    ...
}

Testing

composer test

Commands Usage

Create a new endpoint

php artisan endpoint:make:all Post v1

This will create all this files., (*6)

  • app/Post.php
  • app/Repositories/PostRepository.php
  • app/Transformers/PostTransformer.php
  • app/Http/Controllers/Api/V1/PostController.php

Options:, (*7)

  • --mongo : Generate a Laravel-MongoDB compatible Model. (You need to install https://github.com/jenssegers/laravel-mongodb in your app)
  • --module=Modules\Search : Generate all the files under the App\Modules\Search namespace.
    - app
        - Modules
            - Search
                - Http\Controllers\Api\V1
                - Models
                - Repositories
                - Transformers
    - bootstrap
    - config
    - database
    - ...

Then, you need to register your api routes., (*8)

// routes/api.php
Route::group(['namespace' => 'Api\V1', 'prefix' => 'v1'], function () {
    Route::resource('posts', 'PostController', ['except' => [
        'create', 'edit'
    ]]);
});

Create a new model class.

php artisan endpoint:make:model Post

This will create the file app/Post.php and insert the minimum boilerplate with filtrable trait., (*9)

Optionnaly, you can add the --mongo flag to generate a Laravel-MongoDB compatible Model., (*10)

Create a new controller class.

php artisan endpoint:make:controller Post v1

This will create the file app/Http/Controllers/Api/V1/PostController.php and insert the minimum boilerplate., (*11)

Create a new repository class.

php artisan endpoint:make:repository Post

This will create the file app/Repositories/PostRepository.php and insert the minimum boilerplate., (*12)

Create a new transformer class.

php artisan endpoint:make:transformer Post

This will create the file app/Transformers/PostTransformer.php and insert the minimum boilerplate., (*13)

API Usage

Pagination

Change the selected page

/api/v1/topics?page=2

Change the number of items per page

/api/v1/topics?per_page=50

Returns all

/api/v1/topics?limit=all

Filters

The and filter is applied by default., (*14)

And Filter

/api/v1/topics?filter[]=name,eq,laravel&filter[]=name,eq,eloquent&satisfy=all

Or Filter

/api/v1/topics?filter[]=name,eq,laravel&filter[]=name,eq,eloquent&satisfy=any

Sort

/api/v1/topics?sort=name
/api/v1/topics?sort=-name
/api/v1/topics?sort=-slug,name

Includes

Update your transformer to add your include rules, according to fractal docs (http://fractal.thephpleague.com/transformers/), (*15)

Then you can include related models on your calls, (*16)

/api/v1/topics?include=posts,posts.author

Error responses

  • Bad request (400) $this->badRequest('The request was unacceptable.')
  • Unauthorized (401) $this->unauthorized('No valid API key was provided.')
  • Forbidden (403) $this->forbidden('Access forbidden.')
  • Not found (404) $this->notFound('Resource not found.')
  • Method not allowed (405) $this->methodNotAllowed('The HTTP method is not allowed.')
  • Unprocessable entity (422) $this->unprocessableEntity('Invalid fields.')
  • Too many requests (429) $this->tooManyRequests('Too many requests hit the API.')
  • Server error (500) $this->serverError('Internal server error.')

Note: The ProvidesExceptionsHandler comes with default support for the following exceptions:, (*17)

  • Illuminate\Database\Eloquent\ModelNotFoundException
  • Illuminate\Validation\ValidationException
  • Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException
  • Symfony\Component\HttpKernel\Exception\NotFoundHttpException

Credits

  • http://fractal.thephpleague.com/transformers/
  • https://github.com/spatie/laravel-fractal

License

Laravel Endpoint is open-sourced software licensed under the MIT license, (*18)

The Versions

03/05 2017

dev-master

9999999-dev

Laravel Endpoint is a CRUD REST API package for Laravel.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Fabrice Clementz

laravel api rest search scaffolding

26/04 2017

v0.1.10

0.1.10.0

Laravel Endpoint is a CRUD REST API package for Laravel.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Fabrice Clementz

laravel api rest search scaffolding

04/04 2017

v0.1.9

0.1.9.0

Laravel Endpoint is a CRUD REST API package for Laravel.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Fabrice Clementz

laravel api rest search scaffolding

30/03 2017

v0.1.8

0.1.8.0

Laravel Endpoint is a CRUD REST API package for Laravel.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Fabrice Clementz

laravel api rest search scaffolding

24/03 2017

v0.1.7

0.1.7.0

Laravel Endpoint is a CRUD REST API package for Laravel.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Fabrice Clementz

laravel api rest search scaffolding

19/02 2017

v0.1.6

0.1.6.0

Laravel Endpoint is a CRUD REST API package for Laravel.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Fabrice Clementz

laravel api rest search scaffolding

09/02 2017

v0.1.5

0.1.5.0

Laravel Endpoint is a CRUD REST API package for Laravel.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Fabrice Clementz

laravel api rest search scaffolding

02/02 2017

v0.1.4

0.1.4.0

Laravel Endpoint is a CRUD REST API package for Laravel.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Fabrice Clementz

laravel api rest search scaffolding

31/01 2017

v0.1.3

0.1.3.0

Laravel Endpoint is a CRUD REST API package for Laravel.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Fabrice Clementz

laravel api rest search scaffolding

26/01 2017

v0.1.2

0.1.2.0

Laravel Endpoint is a CRUD REST API package for Laravel.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Fabrice Clementz

laravel api rest search scaffolding

25/01 2017

v0.1.1

0.1.1.0

Laravel Endpoint is a CRUD REST API package for Laravel.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Fabrice Clementz

laravel api rest search scaffolding

25/01 2017

v0.1.0

0.1.0.0

Larapi is a CRUD REST API package for Laravel.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Fabrice Clementz

laravel api rest search scaffolding