This is a personal library to standardise my json response on all my projects.
status
: string 'error' or 'success'.error_msg
: array of errors message.data
: array of data provided.response
: mixed value is possible like object, Class or HTML string, etc.
- Instance the class
$resp = $new JsonResp(/* array of data */);
- Check if your data is valid and add your error message.
if (0 <= $data['number']) $resp->addErrMsg('number must be more than 0');
- Before return response you can check if there are errors.
if ($resp->isError()) die $resp->returnResponse(true);
- 2 ways to return response : JSON string or Array.
$resp->returnResponse(true); // JSON string
$resp->returnResponse(); // Array