alexcane / php-json-response
Personal PHP Class Library for format json response
v1.0.0
2024-10-16 06:35 UTC
Requires
- php: ^7.4 || ^8.0
- ext-json: *
README
PHPJsonResponse
Personal PHP Class Library for format json response
This is a personal library to standardise my json response on all my projects.
Response structure
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.
How to use ?
- 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