Skip to content

navjotsinghprince/laravel-api-responder

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

2 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

⚑ Laravel API Responder

License Packagist Version Laravel Version


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 Key/Value  */ public function example3(Request $request) { $custom_key = "payment_completed"; $custom_value = true; return $response->sendSuccessWith("Your custom success message here...", $custom_key, $custom_value); } }

βš™οΈ 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.