Json response builder library for php.
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
use TanSandbox\JsonRest\Builder;
$builder = new Builder() ;
$data = array (
'name' => 'Nithin',
'subject' => 'English',
'mark' => '90'
);
$builder->ok()->send($data) ;
Will produce
{
"status": true,
"data": {
"name": "Nithin",
"subject": "English",
"mark": "90"
}
}
$data = array(
'name' => 'Please provide a valid name'
);
$builder->fail()->send($data) ;
Will produce
{
"status": false,
"data": {
"name": "Please provide a valid name"
}
}
$data = array(
'reply' => 'Resource not found.'
);
$builder->setStatus(404)->send($data) ;
Will produce
{
"status": false,
"data": {
"reply": "Resource not found."
}
}
$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"
}
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"
}