tyler36 / response-macro
Standardizing JSON responses
Installs: 22
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Type:laravel-plugin
Requires
- php: >=7.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.12
- squizlabs/php_codesniffer: ^3.3
This package is not auto-updated.
Last update: 2024-11-10 05:24:52 UTC
README
These macros help to standardize api json response returns throughout the application. Inspired by Laravel Response Macros for APIs
Installation
- Install package
composer require tyler36/response-macro
Success
Returns payload ($data) with an optional HTTP status code ($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
response()->noContent()
Error
Returns message ($message) content with an optional HTTP status code ($statusCode) [Default: HTTP status code 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!"}