Laravel Rest API management
- Improve your API Response data structure
composer require anhzf/laravel-rest-api
{
"success": true,
"message": "your message here",
"data": {
"myData": "your data here..."
},
"errors": [
"laravel", "automatically", "send", "exceptions", "here..."
]
}
- APIResponse::message()
- APIResponse::data()
- APIResponse::statusCode()
- APIResponse::error()
- Send Shortcut
use Anhzf\LaravelRestAPI\APIResponse;
// end of some controller
return APIResponse::message('[your message here]')->send();
use Anhzf\LaravelRestAPI\APIResponse;
$myData = ['foo', 'bar', 'baz'];
// end of some controller
return APIResponse::data(compact('myData'))->send();
use Anhzf\LaravelRestAPI\APIResponse;
// end of some controller
return APIResponse::statusCode(404)
->message('Didn\'t find matched user!')
->send();
It will send response without success
key in your Json Response
use Anhzf\LaravelRestAPI\APIResponse;
// end of some controller
return APIResponse::error()
->statusCode(422)
->message('email field is required!')
->send();
By adding Send
verb in above listed method, it will be send response like using send()
use Anhzf\LaravelRestAPI;
class APIResponse {
public static sendMessage(string $message);
public static sendData(array $data);
public static sendStatusCode(int $statusCode, bool $success = true);
public static sendError(string $message = null, int $statusCode = JsonResponse::HTTP_BAD_REQUEST);
}
use Anhzf\LaravelRestAPI\APIResponse;
return APIResponse::message('Fetched users data from database')
->sendData(compact('userData'));