Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/tyler36/response-macro
https://github.com/tyler36/response-macro
api helper laravel
Last synced: about 5 hours ago
JSON representation
- Host: GitHub
- URL: https://github.com/tyler36/response-macro
- Owner: tyler36
- License: mit
- Created: 2018-06-07T06:23:11.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2018-07-20T06:04:02.000Z (about 6 years ago)
- Last Synced: 2024-04-19T14:03:21.410Z (6 months ago)
- Topics: api, helper, laravel
- Language: PHP
- Homepage:
- Size: 15.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
Awesome Lists containing this project
README
# Introduction
These macros help to standardize **api json response** returns throughout the application.
Inspired by [Laravel Response Macros for APIs](https://blog.jadjoubran.io/2016/03/27/laravel-response-macros-api/)## Installation
- Install package
```
composer require tyler36/response-macro
```### Success
Returns payload ($data) with an optional [HTTP status code](https://httpstatuses.com/) ($statusCode) [Default: [HTTP status code 200]
```
response()->success($data);
```EG.
```
response()->success(['earth' => 3, 'sun' => 'yellow'])
```
Returns **HTTP status 200** with the following:
```
{"errors":false,"data":{"earth":3,"sun":"yellow"}}
```### noContent
Returns empty content with a [HTTP status code 402](https://httpstatuses.com/204)
```
response()->noContent()
```### Error
Returns message ($message) content with an optional [HTTP status code](https://httpstatuses.com/) ($statusCode) [Default: [HTTP status code 400](https://httpstatuses.com/400)]
```
response()->error($message);
```Eg.
```
response()->error('There was an error.');
```
Returns **HTTP status 400** with the following:
```
{"errors":true,"message":"There was an error."}
```Eg.
```
response()->error('Not authorized!', 403);
```Returns **HTTP status 403** with the following:
```
{"errors":true,"message":"Not authorized!"}
```