// create response instance
$res = new Response();
set your response status accept boolean value true or false
$res->setStatus(false); // default value is false
you can set data using this method accept any format data such as string,object,array,boolean,integer
.
$data = array(
"first_name" => "syed mohammad hussain",
"last_name" => "naqvi",
);
$res->setData($data);
you can set your errors for response using this method accept Error instance or null
data format.
//pass array of errors codes
$res->setErrors([1100, 1101, 1102, 1105]);
// pass errors code with text message
$res->setErrors([
array("code" => 1100, "message" => "user not found!")
]);
// or you can use Error instance to manage better and more comfortable your errors
$errors = new Errors();
$errors->addErrorCode(1100);
$errors->addErrorCode(1101);
$errors->addErrorCode(1102);
$errors->addErrorCode(1105);
$res->setErrors($errors->getErrorsCodes());
return your setup response data.
$data = $res->getData();
return response status boolean value.
$status = $res->getStatus();
return your setup error from response instance.
$errors = $res->getErrors();
in this method set header content-type to application/json for show json response
header('Content-Type: application/json; charset=utf-8');
using exit()
to stop php process and json_encode()
to encode data to json .
$res->toJson();
return array
of response object has success|errors|data
arrays key
$response = $res->toArray();
$success = $response["success"];
$data = $response["data"];
$errors = $response["errors"];
created by @smhnaqvi