navjot / laravel-api-responder
A Reusable Laravel consistent API response package for web and mobile apps
Fund package maintenance!
navjotsinghprince
README
A Reusable Laravel consistent API response package for web and mobile apps
π Introduction
Laravel API Responder is a lightweight Laravel package for generating clean, standardized API responses with minimal code.
It includes pre-configured methods to handle HTTP status codes, success, error, validation failures, and more.
β¨ Features
- β Consistent and clean JSON API response format
- β Predefined HTTP status codes
- β Reusable trait for any controller
- β Works out of the box β no configuration needed
- β PSR-4 & Laravel auto-discovery support
- β Compatible with Laravel 10, 11, and 12+
π¦ Installation
composer require navjot/laravel-api-responder
π₯ Importing ApiResponder
use Navjot\Laravel\ApiResponder;
β Option 1: Use in a Single Controller
namespace App\Http\Controllers; use Illuminate\Http\Request; use Navjot\Laravel\ApiResponder; class TestController extends Controller { use ApiResponder; public function index(Request $request) { return $this->sendSuccess("Response message", ['foo' => 'bar']); } }
π Option 2: Global Import for All Controllers
To enable ApiResponder globally in every controller, add it to Laravelβs default base controller: app/Http/Controllers/Controller.php file.
<?php namespace App\Http\Controllers; use Navjot\Laravel\ApiResponder; abstract class Controller { use ApiResponder; }
π§ Usage Examples
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Navjot\Laravel\ApiResponder; use Illuminate\Support\Facades\Validator; //Feel Free To Visit https://navjotsinghprince.com class TestController extends Controller { use ApiResponder; /** * Example 1... */ public function example1(Request $request) { $collection = collect([1, 2, 3]); $class_obj = new \stdClass(); $class_obj->name="Navjot Singh"; $response = [ "string" => "Navjot Singh", "int" => 1, "float" => 3.14, "boolean" => true, "array" => ["prince", "ferozepuria"], "collection" => $collection, "class_object" => $class_obj, "null_value" => null, "empty_string" => "", "empty_array" => [], ]; return $this->sendSuccess(self::SUCCESS,$response); } /** * Example 2... */ public function example2(Request $request) { $validator = Validator::make($request->all(), [ 'name' => 'required', 'email'=>'required|email' ]); if ($validator->fails()) { return $this->validationFailed(self::ALL_FIELDS_REQUIRED, $validator->errors()); } } /** * Example 3... * Usage With Custom Message */ public function example3(Request $request) { $data = ["name" => "Navjot Singh"]; return $response->sendSuccess("Your custom success message here...", $data); } }
βοΈ Available Methods
<?php $response = [ "name" => "Navjot Singh", "email" => "info@navjotsinghprince.com", "website" => "https://navjotsinghprince.com" ]; return $this->sendSuccess("eg. Request completed successfully", $response); return $this->sendSuccessWith("eg. Request processed successfully","total",$response); return $this->sendFailure("eg. Unable to process your request at this time",$response); return $this->validationFailed("eg. Validation failed. Please check your input",$response); return $this->notFound("eg. The requested resource was not found",$response); return $this->unauthorized("eg. Authentication required. Please log in"); return $this->forbidden("eg. You do not have permission to access this resource"); return $this->badRequest("eg. An error occurred while processing your request");
π‘ Tips
π‘ Tip: Use Available Messages: SUCCESS , FAILED , ALL_FIELDS_REQUIRED , SOMETHING_WRONG , SOMETHING_WRONG_LATER.
π‘ Tip: Best practice: Add the ApiResponder to app/Http/Controllers/Controller.php to avoid importing in every controller manually.
<?php return $this->sendSuccess(self::SUCCESS, [...]); return $this->sendFailure(self::FAILED, [...]); return $this->validationFailed(self::ALL_FIELDS_REQUIRED, [...]); return $this->sendFailure($this::SOMETHING_WRONG, ['same work as self keyword']); return $this->sendFailure($this::SOMETHING_WRONG_LATER, [...]);
π¨βπ» Authors
π Navjot Singh Prince
See also the site of contributor who participated in this package.
π¬ Contact
If you discover any question within package, please send an e-mail to Navjot Singh via info@navjotsinghprince.com. Your all questions will be answered.
π Changelog
Please see changelog.md for what has changed recently.
β Buy Me A Coffee! β
Feel free to buy me a coffee at Buy me a coffee! β, I would be really grateful for anything, be it coffee or just a kind comment towards my work, that helps me a lot.
π° Donation
The package is completely free to use, however, it has taken a lot of time to build. If you would like to show your appreciation by leaving a small donation, you can do so by clicking here here. Thanks!
π License
This project is licensed under the MIT License - see the LICENSE.md file for details.