laravel-api-responses maintained by mpstr24
Laravel API Responses
This package provides API response functions via a trait to help improve consistency of API response codes across your projects.
Features
- Pre-made functions of the most common API responses.
Installation
Install the package via Composer:
composer require mpstr24/laravel-api-responses
Usage/Examples
First add the trait to your controller.
use ApiResponseTrait;
All methods, except from "No Content" allow for a message and data to be returned. Custom messages also allow you to choose the status code.
Examples of each below.
Success - 200
public function success()
{
return $this->apiSuccess();
}
Created - 201
public function created()
{
return $this->apiCreated();
}
Accepted - 202
public function accepted()
{
return $this->apiAccepted();
}
No Content - 204
public function noContent()
{
return $this->apiNoContent();
}
Bad Request - 400
public function badRequest()
{
return $this->apiBadRequest();
}
Unauthorized - 401
public function unauthorized()
{
return $this->apiUnauthorized();
}
Forbidden - 403
public function forbidden()
{
return $this->apiForbidden();
}
Not Found - 404
public function notFound()
{
return $this->apiNotFound();
}
Teapot - 418
public function teapot()
{
return $this->apiTeapot();
}
Unprocessable - 422
public function unprocessable()
{
return $this->apiUnprocessable();
}
Too Many Requests - 429
public function tooManyRequests()
{
return $this->apiTooManyRequests();
}
Server Error - 500
public function serverError()
{
return $this->apiInternalServerError();
}
Custom response
public function apiCustomResponse()
{
return $this->apiCustom('Custom', null, 201);
}
Roadmap
- Support for resources
- Expand into more helpful API features
- Larastan goals
- 5
- 6
- 7
- 8
- 9