Download the PHP package larahub/api-response-kit without Composer
On this page you can find all versions of the php package larahub/api-response-kit. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download larahub/api-response-kit
More information about larahub/api-response-kit
Files in larahub/api-response-kit
Package api-response-kit
Short Description A Laravel package to standardize API responses, errors, and validation outputs across Laravel applications
License MIT
Homepage https://www.larahub.io
Informations about the package api-response-kit
LaraHub API Response Kit
Vendor: LaraHub | Domain: larahub.io | Package: larahub/api-response-kit
A Laravel package to standardize API responses, errors, and validation outputs across Laravel applications.
Why
- Consistent JSON response schema across teams/projects
- Less boilerplate in controllers/services
- Predictable error + validation formats
- Optional request ID for tracing/debugging
- Environment-aware debug mode (hides sensitive info in production)
- Custom formatter support for project-specific schemas
Features
| Feature | Description |
|---|---|
| Automatic response formatting | Wraps controller return values (arrays, models, collections) into standard JSON |
| Pagination support | Auto-detects paginate() / cursorPaginate() results and moves pagination metadata into meta.pagination |
| Global error handling | Converts exceptions into standardized JSON error responses (404, 403, 401, 500, etc.) |
| Validation error standardization | Formats Laravel validation errors into a clean, predictable structure |
| Configurable response schema | Customize response keys via config file |
| Request ID generation | Unique request ID (LH-XXXXXX) per request for debugging/logging |
| Facade support | Manual response control when automatic formatting is not needed |
| Middleware integration | Intercepts outgoing responses and applies formatting automatically |
| Debug mode | Full stack traces in dev; sensitive info (SQL, file paths) hidden in production |
| Custom formatters | Define your own response structure via custom formatter classes |
Installation
Publish Config
This will publish config/api-response-kit.php.
Response Schema
Success Response
Error Response
Validation Error Response
Basic Usage
When middleware is enabled, normal controller returns are automatically wrapped into the standard response schema.
Pagination
Auto-detection (middleware)
When the middleware is enabled, any paginate() or cursorPaginate() return value is automatically detected and reformatted. No changes needed in the controller:
Paginated response schema
Manual usage (Facade)
Using the config per_page helper
Keep your per-page number in one place — config/api-response-kit.php:
Configuration
Publish the config and adjust the default:
Or set it in .env:
Manual Usage (Facade)
Middleware
The package registers a middleware alias:
api-response-kit
Auto-registration is controlled by config keys:
api-response-kit.auto_middlewareapi-response-kit.middleware.enabledapi-response-kit.middleware.globalapi-response-kit.middleware.exclude
Configuration
Customizing Schema Keys and Defaults
Edit config/api-response-kit.php:
keys.*— override key names (success,message,data,errors,meta)default_message— default success messagerequest_id_prefix— prefix for generated request IDs
Debug Mode
In APP_DEBUG=true (development): full error details and stack traces are included.
In production: SQL queries, stack traces, and file paths are hidden.
Custom Formatters
You can provide your own formatter classes via config:
Config keys:
api-response-kit.formatter— default formatter implementationapi-response-kit.custom_formatters— named formattersapi-response-kit.formatters.*— override built-in per type:success/error/validation/exception
For safety/backwards-compatibility, per-type overrides should extend the built-in formatter they replace.
Package Architecture
Folder Structure
Core Components
| Component | Responsibility |
|---|---|
ApiResponseKitServiceProvider |
Register services, publish config, bind into Laravel container |
ApiResponseKitMiddleware |
Intercept responses, detect type, apply appropriate formatter |
SuccessFormatter |
Format successful responses |
ErrorFormatter |
Format generic errors and HTTP exceptions |
ValidationFormatter |
Format validation error responses |
ExceptionFormatter |
Convert exceptions into standardized JSON errors |
ResponseSchema |
Build final response structure based on config |
RequestIdGenerator |
Generate unique request IDs |
ConfigResolver |
Resolve configuration values dynamically |
ApiResponseKit (Facade) |
Manual response control when automatic formatting is not desired |
Testing
The package ships with a PHPUnit test suite powered by Orchestra Testbench, which boots a real Laravel application for each test.
Requirements
| Dependency | Version |
|---|---|
phpunit/phpunit |
^10.0 \| ^11.0 |
orchestra/testbench |
^8.0 \| ^9.0 \| ^10.0 |
Install dev dependencies (first time only):
Run the Test Suite
Or via the composer test script if you add one:
Test Structure
| File | What it covers |
|---|---|
tests/TestCase.php |
Base class — boots the service provider, registers the facade, enables debug mode |
tests/SuccessFormatterTest.php |
SuccessFormatter, ApiResponseKit::success/created/accepted/noContent/paginated, request ID helpers, data normalisation |
tests/ErrorFormatterTest.php |
ErrorFormatter, ValidationFormatter, ExceptionFormatter, all ApiResponseKit error/HTTP-status helpers |
tests/MiddlewareTest.php |
ApiResponseKitMiddleware — pass-through conditions, success wrapping, pagination detection, error/validation formatting, exception catching |
Test Environment
The base TestCase pre-configures the following for every test:
Each test method receives a fresh application instance, so config overrides in one test cannot bleed into another.
Example: Override Config Per Test
Target Users
- Laravel backend developers
- API-driven applications (REST APIs)
- Development teams requiring standardized API contracts
- SaaS and enterprise Laravel projects
Roadmap
v2 (Planned)
- API versioning support
- Multiple response schemas
- Advanced logging integration
v3 (Planned)
- API documentation auto-generation
- Frontend SDK integration
- Enterprise-level customization