https://github.com/myerscode/laravel-api-response
A fluent helper and facade to ensure consistent, idempotent API responses in Laravel and Lumen
https://github.com/myerscode/laravel-api-response
builder fluent-helper laravel response
Last synced: 9 months ago
JSON representation
A fluent helper and facade to ensure consistent, idempotent API responses in Laravel and Lumen
- Host: GitHub
- URL: https://github.com/myerscode/laravel-api-response
- Owner: myerscode
- License: mit
- Created: 2018-05-07T22:42:46.000Z (almost 8 years ago)
- Default Branch: main
- Last Pushed: 2024-09-15T22:55:34.000Z (over 1 year ago)
- Last Synced: 2024-10-30T14:27:00.805Z (over 1 year ago)
- Topics: builder, fluent-helper, laravel, response
- Language: PHP
- Homepage:
- Size: 29.3 KB
- Stars: 4
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGES.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Laravel API Response
> A fluent helper to provide a consistent shaped API responses in Laravel
[](https://packagist.org/packages/myerscode/laravel-api-response)
[](https://packagist.org/packages/myerscode/laravel-api-response)
[](https://packagist.org/packages/myerscode/laravel-api-response)

[](https://codecov.io/gh/myerscode/laravel-api-response)
## Why is this package helpful?
This package ensures your API will always return the same envelope shape, so consuming apps always know what to expect!
## Install
You can install this package via composer:
``` bash
composer require myerscode/laravel-api-response
```
## Usage
In a Laravel controller you just to build up your response and return it!
The `api()` helper return a Response `Builder` and as it implements the [Responsable](https://laravel.com/api/master/Illuminate/Contracts/Support/Responsable.html)
trait you dont need to do anything more than return the builder
### Using the api() helper function
```php
function resource()
{
return api()->status(201)->data(['name' => 'Foo Bar'])->message('Record Created!');
}
```
### Using a Builder class
```php
function resource() {
$buillder = new Builder();
$builder->status(201)->data(['name' => 'Foo Bar'])->message('Record Created!');
return $builder;
}
```
Would return the following `JSON` response.
```json
{
"status": 201,
"data": {
"name": "Foo Bar"
},
"meta": [],
"messages": [
"Record Created!"
]
}
```
## License
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.