Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/sametsahindogan/response-object-creator
Provide to standardized response objects for your API.
https://github.com/sametsahindogan/response-object-creator
json rest-api
Last synced: 3 months ago
JSON representation
Provide to standardized response objects for your API.
- Host: GitHub
- URL: https://github.com/sametsahindogan/response-object-creator
- Owner: sametsahindogan
- License: mit
- Created: 2020-01-21T12:00:46.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2020-01-21T12:19:51.000Z (about 5 years ago)
- Last Synced: 2024-10-12T11:11:41.590Z (3 months ago)
- Topics: json, rest-api
- Language: PHP
- Homepage:
- Size: 3.91 KB
- Stars: 6
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# response-object-creator
[![GitHub license](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](https://raw.githubusercontent.com/sametsahindogan/response-object-creator/master/LICENSE)
>This package provides to standardized response objects for your API.
>One of the important things in an API is that it has a standard response type. This package provides a standard response structure for your API.
## Installation
```bash
composer require sametsahindogan/response-object-creator
```
## Usage**If you want to return a "success" result;**
```php
// Your array that you want to return as an object.
$data = [
'id' => 1,
'name' => 'John',
'surname' => 'Doe'
];// If you use pure PHP;
return json_encode( new SuccessResult($data) );
// If you use Laravel Framework;
return response()->json( new SuccessResult($data) );// If you use Symfony Framework;
return new JsonResponse( new SuccessResult($data) );
```
Output;
```json
{
"success": true,
"data": {
"id": 1,
"name": "John",
"surname": "Doe"
}
}
```
**If you want to return an "error" result;**
```php
// You must create an Error Builder object.
$errorBuilder = new ErrorBuilder();// If you use pure PHP;
return json_encode(new ErrorResult(
$errorBuilder->title('Error')
->message('Something wrong happened.')
->code(1)
->extra(['You can add some extra messages or etc.'])
)
);
// If you use Laravel Framework;
return response()->json(new ErrorResult(
$errorBuilder->title('Error')
->message('Something wrong happened.')
->code(1)
->extra(['You can add some extra messages or etc.'])
)
);// If you use Symfony Framework;
return new JsonResponse(new ErrorResult(
$errorBuilder->title('Error')
->message('Something wrong happened.')
->code(1)
->extra(['You can add some extra messages or etc.'])
)
);
```
Output;
```json
{
"success": false,
"data": {
"message": "User does not have the right roles or permissions.",
"title": "Operation Failed",
"code": 1,
"extra": [
"You can add some extra messages or etc."
]
}
}
```
## License
MIT © [Samet Sahindogan](https://github.com/sametsahindogan/laravel-jwtredis/blob/master/LICENSE)