https://github.com/laratoolbox/responder
Easily send response to api requests for laravel.
https://github.com/laratoolbox/responder
api api-response laravel response
Last synced: 3 months ago
JSON representation
Easily send response to api requests for laravel.
- Host: GitHub
- URL: https://github.com/laratoolbox/responder
- Owner: laratoolbox
- License: mit
- Created: 2021-01-17T10:35:55.000Z (about 5 years ago)
- Default Branch: main
- Last Pushed: 2021-01-26T13:00:42.000Z (about 5 years ago)
- Last Synced: 2025-08-05T19:09:11.350Z (7 months ago)
- Topics: api, api-response, laravel, response
- Language: PHP
- Homepage:
- Size: 13.7 KB
- Stars: 4
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
# Laravel Responder
Easily respond to api requests.
# Requirement
> Laravel >= 5.5
# Installation
You can install the package via composer:
```bash
$ composer require laratoolbox/responder
```
# Usage
After installation, you can use helper `responder` function.
```php
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:
```text
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.
```php
throw new \LaraToolbox\Responder\Exceptions\ResponderException(
\LaraToolbox\Responder\ResponseCodes::ERROR,
$customData = [1,2,3]
);
```
See response below:
```text
HTTP/1.1 200 OK
Content-Type: application/json
{
"code": 120,
"message": "An error occurred",
"data": [
1,
2,
3
]
}
```
### All methods that responder has.
#### send
- Parameter `null|mixed $data` (if set calls setData method behind the scenes)
#### setHttpStatusCode
- Parameter `int $statusCode`
#### setResponseMeta
- Parameter `array $responseMeta`
#### setResponseCode
- Parameter `int $responseCode`
#### setResponseMessage
- Parameter `string $responseMessage`
#### setData
- Parameter `mixed $data`
#### addExtraData
- Parameter `string $key`
- Parameter `mixed $value`
#### addHeader
- Parameter `string $key`
- Parameter `string|integer|numeric $value`
# Testing
// TODO:
# Changelog
Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.
# Contributing
Please see [CONTRIBUTING](CONTRIBUTING.md) for details.
# Security
If you discover any security related issues, please email hasansemiherdogan@gmail.com instead of using the issue tracker.
Credits
- [Semih ERDOGAN](https://github.com/laratoolbox)
- [Dincer DEMIRCIOGLU](https://github.com/dinncer)
- [All contributors](https://github.com/laratoolbox/database-viewer/graphs/contributors)
# License
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.