| Package Data | |
|---|---|
| Maintainer Username: | raditzfarhan |
| Maintainer Contact: | raditzfarhan@gmail.com (Raditz Farhan) |
| Package Create Date: | 2020-04-17 |
| Package Last Update: | 2024-03-05 |
| Language: | PHP |
| License: | MIT |
| Last Refreshed: | 2025-10-27 03:17:18 |
| Package Statistics | |
|---|---|
| Total Downloads: | 3,620 |
| Monthly Downloads: | 18 |
| Daily Downloads: | 0 |
| Total Stars: | 4 |
| Total Watchers: | 1 |
| Total Forks: | 1 |
| Total Open Issues: | 0 |
Laravel and Lumen API response transformer
Via Composer
$ composer require raditzfarhan/laravel-api-response:^1.0
The Laravel and Lumen configurations vary slightly, so here are the instructions for each of the frameworks.
Edit the config/app.php file and add the following line to register the service provider:
'providers' => [
...
RaditzFarhan\ApiResponse\ApiResponseServiceProvider::class,
...
],
Tip: If you're on Laravel version 5.5 or higher, you can skip this part of the setup in favour of the Auto-Discovery feature.
Edit the bootstrap/app.php file and add the following line to register the service provider:
...
$app->register(RaditzFarhan\ApiResponse\ApiResponseServiceProvider::class);
...
You will also need to enable Facades in bootstrap/app.php:
..
$app->withFacades();
...
Example usage as below snippet:
// Success response
// using service container
$response = app('ApiResponse')->success();
// using alias
$response = \ApiResponse::success();
// Failed response
$response = \ApiResponse::failed();
The response will return a Illuminate\Http\Response instance just like when u call response() helper method.
By default, success will use http 200 code if not set, and failed will use http 500 code if not set.
Typical response content as follow:
// success
{
"status": true,
"http_code": 200,
"message": "Success."
}
// failed
{
"status": false,
"http_code": 500,
"message": "Failed."
}
Add/Change payload data by chaining more methods as below:
// Example #1
return ApiResponse::httpCode(201)->message('Created new record!')->data(['name' => 'Raditz Farhan', 'country' => 'MY'])->success();
// Example #2
return ApiResponse::httpCode(422)->message('Validation error!')->errors(['name' => ['Name field is required.']])->failed();
Above call will result in below:
// Example #1
{
"status": true,
"http_code": 201,
"message": "Created new record!",
"data": {
"name": "Raditz Farhan",
"country": "MY"
}
}
// Example #2
{
"status": false,
"http_code": 422,
"message": "Validation error!",
"errors": {
"name": [
"Name field is required."
]
},
}
Please see the changelog for more information on what has changed recently.
MIT. Please see the license file for more information.