jenky/laravel-api

7.2.0 2021-02-20 05:42 UTC

README

Latest Version on Packagist Test Status Codecov Total Downloads Software License

The package provides a nice and easy way to define API routes and format JSON error response.

Install

Installation

You may use Composer to install this package into your Laravel project:

$ composer require jenky/laravel-api

After installing, publish its assets using the vendor:publish Artisan command.

php artisan vendor:publish

or

php artisan vendor:publish --provider="Jenky\LaravelAPI\ApiServiceProvider"

Configuration

After publishing Laravel API's assets, its primary configuration file will be located at config/api.php. This configuration file allows you to configure your api route and error response format and each configuration option includes a description of its purpose, so be sure to thoroughly explore this file.

For Header versioning, if the request doesn't have the Accept header with correct format then default version from config will be used.

Usage

Error response

Dealing with errors when building an API can be a pain. Instead of manually building error responses you can simply throw an exception and the API will handle the response for you. Just add the trait ExceptionResponse to your app/Exceptions/Handler and the package will automatically catches the thrown exception and will convert it into its JSON representation.

<?php

namespace App\Exceptions;

use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Jenky\LaravelAPI\Exception\ExceptionResponse;

class Handler extends ExceptionHandler
{
    use ExceptionResponse;
}

You can change the default error response format in config/api.php file.

Change log

Please see CHANGELOG for more information on what has changed recently.

Testing

$ composer test

Contributing

Please see CONTRIBUTING and CODE_OF_CONDUCT for details.

Security

If you discover any security related issues, please email contact@lynh.me instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.