alexcane/php-json-response

Personal PHP Class Library for format json response

v1.0.0 2024-10-16 06:35 UTC

This package is auto-updated.

Last update: 2025-02-17 22:13:05 UTC


README

Tests License

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 ?

  1. Instance the class
$resp = $new JsonResp(/* array of data */);
  1. Check if your data is valid and add your error message.
if (0 <= $data['number']) $resp->addErrMsg('number must be more than 0');
  1. Before return response you can check if there are errors.
if ($resp->isError()) die $resp->returnResponse(true);
  1. 2 ways to return response : JSON string or Array.
$resp->returnResponse(true); // JSON string
$resp->returnResponse(); // Array