Run the Composer require command from the Terminal:
composer require hankz/laravel-plus-api
This package supports Laravel's auto-discovery feature and it's ready to use once installed.
You need publish the config file.
php artisan vendor:publish --provider="Hankz\LaravelPlusApi\LaravelPlusApiServiceProvider"
Set middleware for routes where you intend to utilize API responses.
in app/Http/Kernel.php
'api' => [
//...
\Hankz\LaravelPlusApi\Middleware\SetHeaderAcceptJson::class,
]
Set the priority of the middleware.
in app/Http/Kernel.php
protected $middlewarePriority = [
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\Hankz\LaravelPlusApi\Middleware\SetHeaderAcceptJson::class,
\App\Http\Middleware\Authenticate::class,
\Illuminate\Routing\Middleware\ThrottleRequests::class,
\Illuminate\Session\Middleware\AuthenticateSession::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
\Illuminate\Auth\Middleware\Authorize::class,
];
Override exception Handler.
in app/Exceptions/Handler.php
,add:
use Hankz\LaravelPlusApi\Classes\ApiResponseBuilder;
class Handler extends ExceptionHandler
{
//...
protected function invalidJson($request, ValidationException $exception)
{
return ApiResponseBuilder::validationError($exception);
}
protected function unauthenticated($request, AuthenticationException $exception)
{
return ApiResponseBuilder::error(
config('laravel-plus-api.default_response.unauthenticated.api_code'),
config('laravel-plus-api.default_response.unauthenticated.http_code'),
config('laravel-plus-api.default_response.unauthenticated.message')
);
}
protected function prepareJsonResponse($request, Throwable $e)
{
return ApiResponseBuilder::exceptionError(
$throwable,
data_get($defaultResponseConfig, 'api_code'),
($throwable instanceof HttpExceptionInterface) ? $throwable->getStatusCode() : data_get($defaultResponseConfig, 'http_code'),
$throwable->getMessage() ? $throwable->getMessage() : data_get($defaultResponseConfig, 'message'),
null,
($throwable instanceof HttpExceptionInterface) ? $throwable->getHeaders() : []
);
}