A simple package for generating API responses in Laravel
MIT License
A simple package for generating API responses in Laravel.
You can install the package via composer:
composer require mrgarest/echo-api
Below are some examples using the methods for different responses.
To create a successful JSON response, you can use the method:
return EchoApi::success();
Response result:
{
"success": true
}
If you want to add additional data to the response, you can pass an array with data to the success()
method.
$data = [
'user' => [
'id' => 21314,
'role' => 'user',
'email' => '[email protected]'
]
];
return EchoApi::success($data);
Response result:
{
"success": true,
"user": {
"id": 21314,
"role": "user",
"email": "[email protected]"
}
}
To create a JSON response with the HTTP error code, you can use the method:
$httpStatus = Response::HTTP_NOT_FOUND; // 404 Not Found
return EchoApi::httpError($httpStatus);
Response result:
{
"success": false,
"error": {
"code": 404,
"message": "Bad Request"
}
}
If the standard HTTP error codes are not enough for you, you can use your own by creating them in the config/echo-api.php
file.
return EchoApi::findError('EXAMPLE');
Response result:
{
"success": false,
"error": {
"code": "EXAMPLE",
"message": "Example of error data structure"
}
}
To get echo-api.php
, don't forget to run php artisan vendor:publish
.
As with the success()
method, you can add additional data to the responses for the httpError()
and findError()
methods.
$data = [
'error' = [
'uuid' => '21e38f4d-3be8-457c-98da-3059a947e75b'
],
'count' => 0,
'data' => null
];
return EchoApi::httpError(Response::HTTP_NOT_FOUND, $data);
Response result:
{
"success": false,
"error": {
"code": 404,
"message": "Bad Request",
"uuid": "21e38f4d-3be8-457c-98da-3059a947e75b"
},
"count": 0,
"data": null
}