fabianomendesdev / laravel-json-response
Custom Response Structure for a Laravel API
v1.1.1
2024-12-07 06:05 UTC
Requires
- php: ^8.0
- illuminate/contracts: ^6.0 || ^7.0 || ^8.0 || ^9.0 || ^10.0 || ^11.0
- illuminate/database: ^6.0 || ^7.0 || ^8.0 || ^9.0 || ^10.0 || ^11.0
- illuminate/http: ^6.0 || ^7.0 || ^8.0 || ^9.0 || ^10.0 || ^11.0
- illuminate/support: ^6.0 || ^7.0 || ^8.0 || ^9.0 || ^10.0 || ^11.0
- illuminate/validation: ^6.0 || ^7.0 || ^8.0 || ^9.0 || ^10.0 || ^11.0
- symfony/http-kernel: ^5.0 || ^6.0 || ^7.0
This package is auto-updated.
Last update: 2025-02-07 06:28:45 UTC
README
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); }); }