This package is based on "spatie/laravel-query-builder" allows you to rapidly creating API controllers for your Laravel application. This package also works with authorization policies.
Create a new API controller: ProductApiController
:
use DevLabor\Api\Http\Controllers\ApiController;
// ...
class ProductApiController extends ApiController
{
// ...
}
Extend your API routes within routes/api.php
:
// ...
Route::resource('products', 'Api\ProductApiController');
Sometimes you need an identifcation for the object in your api. For this reason, you can use the DevLabor\Api\Http\Resources\ApiResource
as base class for your own resource classes:
use DevLabor\Api\Http\Resources\ApiResource;
class Product extends ApiResource
{
//
}
You can install the package via composer:
composer require devlabor/laravel-api
You can optionally publish the config file with:
php artisan vendor:publish --tag=api
This is the contents of the published config file:
return [
'pagination' => [
/**
* Number of items per page returned in index()
*/
'items' => 20
]
];
By adapting the $authorizeAbilities
member in the controller class, the authorization check can be partially restricted.
// ...
protected $authorizeAbilities = [
'viewAny', // index
'view', // show
'store',
'update',
'destroy'
];
For more information about policies, take a look at Laravel's Creating Policies
You are able to disable the complete check with following member change in your controller class.
protected $authorizeAbilities = false;
/**
* Models location
* @var string
*/
protected $modelPath = 'App\\Models\\';
/**
* Resources location
* @var string
*/
protected $resourcePath = 'App\\Http\\Resources\\';
composer test
Please see CHANGELOG for more information what has changed recently.
Please see CONTRIBUTING for details.
If you discover any security related issues, please email office@devlabor.com instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.