https://github.com/mrgarest/echo-api
A simple package for generating API responses in Laravel
https://github.com/mrgarest/echo-api
api json laravel laravel-package php
Last synced: 4 months ago
JSON representation
A simple package for generating API responses in Laravel
- Host: GitHub
- URL: https://github.com/mrgarest/echo-api
- Owner: mrgarest
- License: mit
- Created: 2024-09-19T10:05:48.000Z (9 months ago)
- Default Branch: master
- Last Pushed: 2024-12-01T20:35:37.000Z (6 months ago)
- Last Synced: 2025-01-30T16:43:26.392Z (4 months ago)
- Topics: api, json, laravel, laravel-package, php
- Language: PHP
- Homepage:
- Size: 5.86 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# EchoApi
A simple package for generating API responses in Laravel.
## Installation
You can install the package via composer:
```
composer require mrgarest/echo-api
```# Examples
Below are some examples using the methods for different responses.
## Success
To create a successful JSON response, you can use the method:
```php
return EchoApi::success();
```Response result:
```json
{
"success": true
}
```### Adding additional data
If you want to add additional data to the response, you can pass an array with data to the `success()` method.
```php
$data = [
'user' => [
'id' => 21314,
'role' => 'user',
'email' => '[email protected]'
]
];return EchoApi::success($data);
```Response result:
```json
{
"success": true,
"user": {
"id": 21314,
"role": "user",
"email": "[email protected]"
}
}
```## Error
To create a JSON response with the HTTP error code, you can use the method:
```php
$httpStatus = Response::HTTP_NOT_FOUND; // 404 Not Found
return EchoApi::httpError($httpStatus);
```Response result:
```json
{
"success": false,
"error": {
"code": 404,
"message": "Bad Request"
}
}
```### Custom error
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.
```php
return EchoApi::findError('EXAMPLE');
```Response result:
```json
{
"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`.*
### Adding additional data
As with the `success()` method, you can add additional data to the responses for the `httpError()` and `findError()` methods.
```php
$data = [
'error' = [
'uuid' => '21e38f4d-3be8-457c-98da-3059a947e75b'
],
'count' => 0,
'data' => null
];
return EchoApi::httpError(Response::HTTP_NOT_FOUND, $data);
```Response result:
```json
{
"success": false,
"error": {
"code": 404,
"message": "Bad Request",
"uuid": "21e38f4d-3be8-457c-98da-3059a947e75b"
},
"count": 0,
"data": null
}
```