composer require twom/laravel-responder
You must add the service provider to config/app.php
for load lang
'providers' => [
// for laravel 5.8 and below
\Twom\Responder\ResponderServiceProvider::class,
];
{
"status": "success or error",
"status_code": 200,
"message": "your setted message",
"data": "your data",
"errors": "your errors"
}
Note: support of fa and en languages.
method | description |
---|---|
respond(array $data = []) |
default is successfully respond. merge $data with default schema |
respondValidationError($errors = []) |
validation error code 422 |
respondCreated($data = null) |
created code 201. you can set data from this method input |
respondUpdated($data = null) |
updated code 200. you can set data from this method input |
respondDeleted() |
deleted successfully response with code 200 |
respondBadRequest() |
response bad request error code 400 |
respondNotFound() |
response not found page error code 404 |
respondInternalError() |
response internal error code 500 |
respondUnauthorizedError() |
response unauthorize error code 401 |
method | description |
---|---|
setStatusCode($statusCode) |
set the status_code in default schema |
setMessage($message, $mode = null) |
set the message in default schema. the $mode parameter can be `created |
setRespondData($data) |
set the data in default schema |
appendRespondData($data) |
append the data in default schema |
setRespondError($error) |
set the errors in default schema |
Note: you must use of this facade.
use Twom\Responder\Facade\Responder; // use of this
return Responder::respond();
output:
{
"status_code": 200,
"status": "success",
"message": "operation successfully!"
}
Note: default mode is successfully operation.
return Responder::respond([
"status" => "my status",
"status_code"=> 300,
"message" => "custom message",
]);
output:
{
"status_code": 300,
"status": "my status",
"message": "custom message"
}
return Responder::respondValidationError([
'title' => [
'the title field is required.', // ...another errors
], // ... another fields
]);
output:
{
"status_code": 422,
"status": "error",
"errors": {
"title": [
"the title field is required."
]
},
"message": "validation error!"
}
return Responder::respondCreated();
// or
return Responder::respondCreated("the created object");
output:
{
"status_code": 201,
"status": "success",
"data": "the created object",
"message": "operation successfully!"
}
return Responder::respondBadRequest();
output:
{
"status_code": 400,
"status": "error",
"message": "operation has error!"
}
return Responder::setMessage("test message")
->setRespondData(["the data"])
->setRespondError(["title"=> ["errors"]])
->setStatusCode(203)
->respond();
output:
{
"status_code": 203,
"status": "success",
"message": "test message",
"data": [
"the data"
],
"errors": {
"title": [
"errors"
]
}
}