laravel-json-response maintained by fabianomendesdev
Description
Custom Response Structure for a Laravel API
Author
Last update
2025/08/05 23:57
(1.x-dev)
License
Downloads
742
laravel-json-response
composer require fabianomendesdev/laravel-json-response
use Fabianomendesdev\LaravelJsonResponse\Api;
return Api::personalizedResponse(200, [
'message' => "Sucesso!",
'response' => new UserResource($user)
]);
$validator = Validator::make($request->all(), [
'nome' => 'required|string|max:100'
]);
if ( $validator->fails() ) {
return Api::personalizedResponse(300, [
'message' => Api::errorFeedbackMessage($validator),
'errors' => $validator->errors()->messages()
]);
}
return Api::successMessage("Success!");
$validator = Validator::make($request->all(), [
'nome' => 'required|string|max:100'
]);
if ($validator->fails()) return Api::returnErrorFields($validator);
return Api::message("Success!", 200);
try {
if (100 > 200) {
throw new Exception("Error");
}
} catch (Exception $e) {
return Api::systemStandardError(Throwable $e);
}
Exception in Laravel "Exceptions/Handler.php"
public function register(): void
{
$this->renderable(function (NotFoundHttpException $notFoundHttpException) {
return Api::standardErrorNotFound($notFoundHttpException);
f});
$this->renderable(function (MethodNotAllowedHttpException $methodNotAllowedHttpException) {
return Api::errorMessage($methodNotAllowedHttpException, "Método não suportado para a rota atual.", 405);
});
$this->renderable(function (Throwable $exception) {
return Api::exception($exception);
});
}