devlabor/laravel-api

Laravel Package for building REST API rapidly.

v2.0.1 2022-07-26 09:51 UTC

This package is auto-updated.

Last update: 2024-10-26 14:59:54 UTC


README

Latest Version on Packagist GitHub Tests Action Status Sonarcloud Status Total Downloads

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.