Download the PHP package i3rror/lapi-response without Composer
On this page you can find all versions of the php package i3rror/lapi-response. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download i3rror/lapi-response
More information about i3rror/lapi-response
Files in i3rror/lapi-response
Package lapi-response
Short Description API Response trait for Laravel, which helps you to create consistent and standardized API responses in your Laravel applications.
License MIT
Informations about the package lapi-response
Laravel API Response
Overview
LAPI-response is a comprehensive Laravel package that standardizes API responses across your application. It provides consistent response formatting, error handling, validation support, and pagination, making it easier to build robust APIs.
Features
- Consistent JSON Response Format: Standardized structure for all API responses
- Multiple Response Types: Support for success, error, validation, and pagination responses
- Error Handling: Built-in error handling with customizable error codes
- Validation Support: Simplified request validation with standardized error responses
- Pagination Support: Easy pagination with meta information
- Stream Response: Support for streaming large datasets
- Configurable: Extensive configuration options to customize behavior
Requirements
- PHP 8.1 or higher (required for PHP enums)
- Laravel 10.43.0 or higher
Dependencies
This package has been optimized to use the minimal set of Laravel components:
Core Dependencies
- illuminate/config
- illuminate/contracts
- illuminate/http
- illuminate/pagination
- illuminate/support
- illuminate/validation
- symfony/http-foundation
- psr/log
Optional Dependencies
The following packages are suggested for specific features:
- illuminate/auth - Required for authentication exception handling
- illuminate/container - Required for standalone usage outside of Laravel
- illuminate/routing - Required for redirect functionality
- symfony/http-kernel - Required for HTTP exception handling
- symfony/console - Required for console commands
When used within a Laravel application, these optional dependencies will be available through Laravel itself.
Installation
Step 1: Install via Composer
Step 2: Register Service Provider
Include the service provider in your config/app.php or in bootstrap/providers.php if you're using Laravel 11:
Step 3: Publish Configuration
Run the following command to publish the package configuration:
Basic Implementation
To use this package, add the APIResponseTrait to your controllers:
You have two implementation options:
- Global Implementation: Add the trait to
App\Http\Controllers\Controller.phpto make it available across all controllers - Local Implementation: Add the trait only to specific controllers where API responses are needed
Helper Functions
Alternatively, you can use the package's global helper functions without adding the trait to your controllers. These functions can be used anywhere in your application:
These helper functions can be used as public functions or as internal helper functions within your application's codebase.
Usage Examples
Basic Response
Response:
Message Example:
Response:
Data Example:
Response:
Stream Response
The stream response feature allows you to handle large datasets efficiently:
Error Handling
Not Found Example
Response:
Bad Request Example
With Error Code
Available Status Types
created- 201 Createdaccepted- 202 Acceptednotfound- 404 Not Foundconflict- 409 Conflictbadrequest- 400 Bad Requestexception- 422 Unprocessable Entityunauthenticated- 401 Unauthorizedunauthorized- 401 Unauthorizedforbidden- 403 Forbiddenok- 200 OK
Validation Support
If validation fails, it returns a standardized error response with validation errors.
Also we have a trait ready to use in case you're using FormRequests.
Pagination Support
The response includes pagination metadata with page information and navigation links.
Available Methods
All methods listed below are available both as trait methods and as global helper functions. You can use them either way depending on your implementation preference.
Create Response
Simplified Usage for apiResponse function
You can use short parameter values in two ways:
- String parameters are set as messages
- Array parameters are set as data
`
Example
Response
Success Responses
Error Responses
Pagination and Validation
Other Utilities
Configuration
The package provides extensive configuration options in config/response.php:
Data Handling
Error Codes
Publishing Error Codes Enum
With custom class name:
Contributors
Testing
This package includes a comprehensive test suite. To run the tests:
Test Coverage
The test suite covers:
- Basic API responses (success, error, etc.)
- Pagination functionality
- Validation handling
- Error code handling
- Exception handling
- Helper functions
License
The MIT License (MIT). Please see License File for more information.
All versions of lapi-response with dependencies
illuminate/config Version >=10.43.0
illuminate/contracts Version >=10.43.0
illuminate/http Version >=10.43.0
illuminate/pagination Version >=10.43.0
illuminate/support Version >=10.43.0
illuminate/validation Version >=10.43.0
symfony/http-foundation Version ^6.4.41 || ^7.4.13 || ^8.1.0
psr/log Version ^3.0