Easily respond to api requests.
Laravel >= 5.5
You can install the package via composer:
$ composer require laratoolbox/responder
After installation, you can use helper responder
function.
return responder()
->addHeader('X-Secret1', 'secret1')
->addHeader('X-Secret2', 'secret2')
->addHeader('X-Secret3', 'secret3')
->setData(\App\Models\User::select('id', 'name')->find(1))
->addExtraData('custom-key', 'custom-value')
->send();
See response below:
HTTP/1.1 200 OK
Content-Type: application/json
X-Secret1: secret1
X-Secret2: secret2
X-Secret3: secret3
{
"code": 0,
"message": null,
"data": {
"id": 1,
"name": "Semih ERDOGAN"
},
"custom-key": "custom-value"
}
Also, you have ResponderException
that you can throw it any time required.
throw new \LaraToolbox\Responder\Exceptions\ResponderException(
\LaraToolbox\Responder\ResponseCodes::ERROR,
$customData = [1,2,3]
);
See response below:
HTTP/1.1 200 OK
Content-Type: application/json
{
"code": 120,
"message": "An error occurred",
"data": [
1,
2,
3
]
}
- Parameter
null|mixed $data
(if set calls setData method behind the scenes)
- Parameter
int $statusCode
- Parameter
array $responseMeta
- Parameter
int $responseCode
- Parameter
string $responseMessage
- Parameter
mixed $data
- Parameter
string $key
- Parameter
mixed $value
- Parameter
string $key
- Parameter
string|integer|numeric $value
// TODO:
Please see CHANGELOG for more information what has changed recently.
Please see CONTRIBUTING for details.
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
Credits
The MIT License (MIT). Please see License File for more information.