https://github.com/gridonic/jsonresponse
Nice structured JsonResponses for the HttpFoundation-Component of Symfony
https://github.com/gridonic/jsonresponse
Last synced: about 1 year ago
JSON representation
Nice structured JsonResponses for the HttpFoundation-Component of Symfony
- Host: GitHub
- URL: https://github.com/gridonic/jsonresponse
- Owner: gridonic
- License: mit
- Created: 2014-10-17T15:23:27.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2016-10-26T07:06:38.000Z (over 9 years ago)
- Last Synced: 2024-11-18T09:39:36.950Z (over 1 year ago)
- Language: PHP
- Size: 22.5 KB
- Stars: 2
- Watchers: 12
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# JsonResponse
Provides simple classes for JSON responses that adhere to a standardized structure.
Since JSON responses may have very different formats, this package supports the specific
[JSend](http://labs.omniti.com/labs/jsend) format defined at http://labs.omniti.com/labs/jsend.
This package is an extension of the [HttpFoundation\JsonResponse](http://symfony.com/doc/current/components/http_foundation/introduction.html)
class from the [Symfony](http://symfony.com/) package.
[](https://travis-ci.org/gridonic/JsonResponse)
## Install
The recommended way to install JsonResponse is [through composer](http://getcomposer.org).
You can either add it as a depedency to your project using the command line:
$ composer require gridonic/json-response
or by adding it directly to your ```composer.json``` file:
```json
{
"require": {
"gridonic/json-response": "1.*"
}
}
```
Run these two commands to install it:
$ curl -s http://getcomposer.org/installer | php
$ php composer.phar install
Now you can add the autoloader, and you will have access to the library:
```php
array(
'id' => 1,
'title' => 'A blog post',
)
);
$message = 'The Blog post was successfully created.';
$title = 'Successfully created!';
$statusCode = 205;
new SuccessJsonResponse($data, $message, $title, $statusCode);
```
```json
{
"status" : "success",
"data" : {
"post": {
"id" : 1,
"title" : "A blog post"
}
},
"message" : "The Blog post was successfully created.",
"title" : "Successfully created!"
}
```
### SuccessJsonResponse
Use a `Gridonic\JsonResponse\ErrorJsonResponse` to build a structured JSON Response.
#### ErrorJsonResponse with Message
```php
$message = 'Oups, data is missing.';
new ErrorJsonResponse(null, $message); // you have to send a message!
```
```json
{
"status" : "error",
"data" : null,
"message" : "Oups, data is missing"
}
```
#### ErrorJsonResponse with Content
```php
$data = array(
'post' => array(
'title' => 'A blog post',
)
);
$message = 'Oups, data is not correct.';
$title = 'An error occured!';
$statusCode = 400;
$errorCode = e311;
$errors = array(
'body' => 'The parameter is missing.',
'title' => 'This parameter is too long.'
);
new ErrorJsonResponse($data, $message, $title, $statusCode, $errorCode, $errors);
```
```json
{
"status" : "error",
"data" : {
"post": {
"title" : "A blog post"
}
},
"message" : "Oups, data is missing",
"title" : "An error occured",
"error_code" : "e311",
"errors" : {
"body" : "The parameter is missing.",
"title" : "This parameter is too long."
}
}
```
## Major & Minor [Releases](https://github.com/gridonic/JsonResponse/releases)
##### 1.1.0
New structure of the responses
##### 1.0.0
Initial Release
## Licence
The JsonResponse is licensed under the [MIT license](LICENSE).