Package Data | |
---|---|
Maintainer Username: | Willywes |
Maintainer Contact: | alejandro.isla.c@gmail.com (Alejandro Isla) |
Package Create Date: | 2020-10-22 |
Package Last Update: | 2023-09-12 |
Home Page: | |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-11-17 03:08:29 |
Package Statistics | |
---|---|
Total Downloads: | 1,318 |
Monthly Downloads: | 10 |
Daily Downloads: | 0 |
Total Stars: | 0 |
Total Watchers: | 2 |
Total Forks: | 1 |
Total Open Issues: | 0 |
Class to generate a standard structure for api json responses.
Via Composer
$ composer require willywes/apiresponse
use Willywes\ApiResponse\ApiResponse;
Default functions that always return a http 200 code, but have a control state.
|Param |Description | |---------------|---------------------------------------| |data |array of data for response (allow null)| |message |custom message to response (optional) | |title |custom title to response (optional) |
|Function |Description | |--------------------------------|-------------------------------------------------| |JsonSuccess | Response status "success" with HTTP 200 | |JsonError | Response status "error" with HTTP 200 | |JsonWarning | Response status "warning" with HTTP 200 | |JsonInfo | Response status "info" with HTTP 200 | |JsonFieldValidation | Response status "field_validation" with HTTP 200|
//Execution in php
return ApiResponse::JsonSuccess([
'user' => User::first(),
'roles' => Role::all(),
]);
//Response
{
"status":"success",
"title":"Operación Exitosa.",
"message": null,
"data":{
"user":{
"id":1,
"full_name":"John Smith",
"email":"jsmith@test.cl",
"role_id":1
},
"roles":[
{
"id":1,
"name":"God Admin"
},
{
"id":2,
"name":"Administrator"
}
]
}
}
//HTTP Response
Status Code: 200 OK
//Execution in php
return ApiResponse::JsonError(null, 'something has gone wrong!', 'oops');
//Response
{
"status":"error",
"title":"oops",
"message":"something has gone wrong!",
"data": null
}
//HTTP Response
Status Code: 200 OK
Default functions that returns a specific http code, but in the same way the body responds
|Param |Description | |---------------|---------------------------------------| |data |array of data for response (allow null)| |message |custom message to response (optional) |
|Function |Description | |--------------------------------|-------------------------------------------------| |Ok | Response status "error" with HTTP 200 | |BadRequest | Response status "error" with HTTP 400 | |Unauthorized | Response status "error" with HTTP 401 | |Forbidden | Response status "error" with HTTP 403 | |NotFound | Response status "error" with HTTP 404 | |InternalServerError | Response status "error" with HTTP 500 | |NotImplemented | Response status "error" with HTTP 501 | |BadGateway | Response status "error" with HTTP 502 |
//Execution in php
return ApiResponse::NotFound(null, 'object not found!');
//Response
{
"status": "error",
"message": "Not Found",
"data": null
}
//HTTP Response
Status Code: 404 Not Found
//Execution in php
return ApiResponse::Unauthorized(null);
//Response
{
"status": "error",
"message": "Unauthorized",
"data": null
}
//HTTP Response
Status Code: 401 Unauthorized
license. Please see the license file for more information.