Introduction
These macros help to standardize api json response returns throughout the application.
Inspired by Laravel Response Macros for APIs, (*1)
Installation
composer require tyler36/response-macro
Success
Returns payload ($data) with an optional HTTP status code ($statusCode) [Default: [HTTP status code 200], (*2)
response()->success($data);
EG., (*3)
response()->success(['earth' => 3, 'sun' => 'yellow'])
Returns HTTP status 200 with the following:, (*4)
{"errors":false,"data":{"earth":3,"sun":"yellow"}}
noContent
Returns empty content with a HTTP status code 402, (*5)
response()->noContent()
Error
Returns message ($message) content with an optional HTTP status code ($statusCode) [Default: HTTP status code 400], (*6)
response()->error($message);
Eg., (*7)
response()->error('There was an error.');
Returns HTTP status 400 with the following:, (*8)
{"errors":true,"message":"There was an error."}
Eg., (*9)
response()->error('Not authorized!', 403);
Returns HTTP status 403 with the following:, (*10)
{"errors":true,"message":"Not authorized!"}