Package Data | |
---|---|
Maintainer Username: | wangchristine |
Package Create Date: | 2021-08-28 |
Package Last Update: | 2022-01-21 |
Home Page: | |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-11-18 03:00:51 |
Package Statistics | |
---|---|
Total Downloads: | 29 |
Monthly Downloads: | 0 |
Daily Downloads: | 0 |
Total Stars: | 1 |
Total Watchers: | 1 |
Total Forks: | 0 |
Total Open Issues: | 0 |
This package gives you an easier way to return RESTful API response.
Install by composer
$ composer require chhw/api-response
If you are under Laravel 5.5, please add this code in config/app.php
below.
<?php
'providers' => [
CHHW\ApiResponse\ApiResponseServiceProvider::class,
],
?>
[Option] Get response config to customize debug message.
$ php artisan vendor:publish --provider="CHHW\ApiResponse\ApiResponseServiceProvider"
In bootstrap/app.php
, you should:
$app->register(CHHW\ApiResponse\ApiResponseServiceProvider::class);
[Option] Get response config to customize debug message.
$ cp vendor/chhw/api-response/src/config/response.php config/response.php
Should be used in your controller.
You can choose what you prefer:
$this->response = new ApiResponse
ApiResponse $response
General
// You can set header and option in construct.
$this->response->setHeader(["lang" => "en"])->setOption(JSON_UNESCAPED_UNICODE);
// Basic usage.
return $this->response->success([1, 2])->json();
return $this->response->error("Oh no")->json();
// Custom status and code.
return $this->response->success([1, 2], 201, "code201")->json();
return $this->response->error("Oh no", 501, "code501")->json();
Inline
return $this->response->success([1, 2])->setHeader(["lang" => "en"])->setOption(JSON_UNESCAPED_UNICODE)->json();
return $this->response->error("Oh no", 501, "code501")->setHeader(["lang" => "en"])->setOption(JSON_UNESCAPED_UNICODE)->json();
If you have custom code, by default it will match config/response.php
settings for debug message:
// Controller
return $this->response->error([1, 2], 500, 'custom500')->json();
// config/response.php
return [
'code' => [
'custom500' => 'Internal Server Error',
]
];
Success
{
"success": true,
"detail": {
"status": 200,
"code": "200",
"message": "OK"
},
"data": {
...
},
"link": null,
"meta": null
}
Error
{
"success": false,
"detail": {
"status": 404,
"code": "404",
"message": "Something went wrong."
},
"error": {
...
},
"link": null,
"meta": null
}
$data can be array, string or object etc.
$code for someone who want to custom internal http code.
public function success($data = [], $status = 200, $code = "200");
public function error($data = [], $status = 500, $code = "500");
public function setHeader($headers);
public function setOption($options);
Response header with an unique UUID4:
X-Trace-Id
Collection Paginator usage just like model !
You can use Api Response and Collection Paginator together.
collect([1,2,3,4,5])->paginate(10)
collect([1,2,3,4,5])->simplePaginate(15)
paginate($perPage = 15, $pageName = 'page', $page = null);
simplePaginate($perPage = 15, $pageName = 'page', $page = null);