tan-sandbox / json-rest
Json response builder.
0.8.2
2017-07-28 09:45 UTC
Requires
- php: >=5.3.0
This package is not auto-updated.
Last update: 2025-08-17 06:42:22 UTC
README
Json response builder library for php.
Installation
Composer
Add to your composer.json or create a new composer.json:
{ "require": { "tansandbox/json-rest": "*" } }
Tell the composer to download the library by running the command:
$ php composer.phar install
To include using compser require, run the following command from your project.
$ php composer.phar require tansandbox/json-rest
Basic usages
Creating object
use TanSandbox\JsonRest\Builder; $builder = new Builder() ;
Returning a success response
$data = array ( 'name' => 'Nithin', 'subject' => 'English', 'mark' => '90' ); $builder->ok()->send($data) ;
Will produce
{ "status": true, "data": { "name": "Nithin", "subject": "English", "mark": "90" } }
Return a failure
$data = array( 'name' => 'Please provide a valid name' ); $builder->fail()->send($data) ;
Will produce
{ "status": false, "data": { "name": "Please provide a valid name" } }
Response with custom http status
$data = array( 'reply' => 'Resource not found.' ); $builder->setStatus(404)->send($data) ;
Will produce
{ "status": false, "data": { "reply": "Resource not found." } }
Advanced response
$data = array ( 'name' => 'Nithin', 'subject' => 'English', 'mark' => '90' ); $builder->setMessage('Action completed')->setStatus(200)->send($data) ;
Will produce
{ "status": false, "data": { "name": "Nithin", "subject": "English", "mark": "90" }, "message": "Action completed" }
Method chaining.
New json member can be added using the setXXX methods. The sendie() method is to output the reponse and to die after that.
$builder->setName('Nithin') ->setSubject('English') ->setMark('90') ->setCustomNotes('Student of ZF2') ->sendie() ;
Will produce
{ "status": true, "message": "Action completed", "name": "Nithin", "subject": "English", "mark": "90", "customNotes": "Student of ZF2" }