devlabor / laravel-api
Laravel Package for building REST API rapidly.
Installs: 1 178
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 3
Forks: 0
Open Issues: 0
Requires
- php: ^8.0
- ext-json: *
- spatie/laravel-query-builder: ^5.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.0
- orchestra/testbench: ^7.0
- phpunit/phpunit: ^9.0
- psalm/plugin-laravel: ^2.0
- vimeo/psalm: ^4.0
This package is auto-updated.
Last update: 2024-10-26 14:59:54 UTC
README
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.
Basic Usage
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 { // }
Installation
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 ] ];
Usage
Configure policy authorization
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
Disable policy authorization
You are able to disable the complete check with following member change in your controller class.
protected $authorizeAbilities = false;
Configure custom paths
/** * Models location * @var string */ protected $modelPath = 'App\\Models\\'; /** * Resources location * @var string */ protected $resourcePath = 'App\\Http\\Resources\\';
Testing
composer test
Changelog
Please see CHANGELOG for more information what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email office@devlabor.com instead of using the issue tracker.
License
The MIT License (MIT). Please see License File for more information.