Package Data | |
---|---|
Maintainer Username: | SDV Plurimédia |
Maintainer Contact: | f.clementz@sdv.fr (Fabrice Clementz) |
Package Create Date: | 2017-01-22 |
Package Last Update: | 2023-10-16 |
Home Page: | |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-12-16 15:01:50 |
Package Statistics | |
---|---|
Total Downloads: | 18,790 |
Monthly Downloads: | 1 |
Daily Downloads: | 0 |
Total Stars: | 11 |
Total Watchers: | 5 |
Total Forks: | 1 |
Total Open Issues: | 7 |
Laravel Endpoint is a CRUD REST API package for Laravel.
You can pull in the package via composer:
$ composer require sdv/laravel-endpoint
Register the service provider.
// config/app.php
'providers' => [
...
SdV\Endpoint\EndpointServiceProvider::class,
]
Replace the render method in app/Exceptions/Handler.php
.
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);
}
...
}
composer test
php artisan endpoint:make:all Post v1
This will create all this files.
Options:
- app
- Modules
- Search
- Http\Controllers\Api\V1
- Models
- Repositories
- Transformers
- bootstrap
- config
- database
- ...
Then, you need to register your api routes.
// routes/api.php
Route::group(['namespace' => 'Api\V1', 'prefix' => 'v1'], function () {
Route::resource('posts', 'PostController', ['except' => [
'create', 'edit'
]]);
});
php artisan endpoint:make:model Post
This will create the file app/Post.php
and insert the minimum boilerplate with filtrable trait.
Optionnaly, you can add the --mongo flag to generate a Laravel-MongoDB compatible Model.
php artisan endpoint:make:controller Post v1
This will create the file app/Http/Controllers/Api/V1/PostController.php
and insert the minimum boilerplate.
php artisan endpoint:make:repository Post
This will create the file app/Repositories/PostRepository.php
and insert the minimum boilerplate.
php artisan endpoint:make:transformer Post
This will create the file app/Transformers/PostTransformer.php
and insert the minimum boilerplate.
/api/v1/topics?page=2
/api/v1/topics?per_page=50
/api/v1/topics?limit=all
The and
filter is applied by default.
/api/v1/topics?filter[]=name,eq,laravel&filter[]=name,eq,eloquent&satisfy=all
/api/v1/topics?filter[]=name,eq,laravel&filter[]=name,eq,eloquent&satisfy=any
/api/v1/topics?sort=name
/api/v1/topics?sort=-name
/api/v1/topics?sort=-slug,name
Update your transformer to add your include rules, according to fractal docs (http://fractal.thephpleague.com/transformers/)
Then you can include related models on your calls
/api/v1/topics?include=posts,posts.author
$this->badRequest('The request was unacceptable.')
$this->unauthorized('No valid API key was provided.')
$this->forbidden('Access forbidden.')
$this->notFound('Resource not found.')
$this->methodNotAllowed('The HTTP method is not allowed.')
$this->unprocessableEntity('Invalid fields.')
$this->tooManyRequests('Too many requests hit the API.')
$this->serverError('Internal server error.')
Note: The ProvidesExceptionsHandler
comes with default support for the following exceptions:
Laravel Endpoint is open-sourced software licensed under the MIT license