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
Informations about the package lapi-response
Laravel API Response
This package provides comprehensive functionality for handling and returning all types of API responses in Laravel applications. It offers consistent response formatting, error handling, and pagination support.
Installation and Setup
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 utilize this package, you'll need to use the APIResponseTrait in your controllers:
You have two implementation options:
- Global Implementation: Add the trait to
App\Http\Controllers\Controller.php
to make it available across all controllers - Local Implementation: Add the trait only to specific controllers where API responses are needed
Usage Examples
Basic Response Example
Expected response:
Simplified Parameter Usage
You can use short parameter values in two ways:
- String parameters are set as messages
- Array parameters are set as data
Message Example:
Response:
Data Example:
Response:
API Stream Response
The stream response feature requires a Generator class as the first parameter:
- Generator is required
- Message is optional
- Status code is optional
Response:
Error Handling
Not Found Exception
Important Notes:
- You can use dash-separated words (e.g., 'not-found')
- Status codes can be used directly (e.g., 404)
Response:
Available Status Types
- created
- accepted
- notfound
- conflict
- badrequest
- exception
- unauthenticated
- unauthorized
- ok
API Response Parameters
The apiResponse function accepts the following arguments:
type
=> Response type (from the types listed above)filter_data
=> booleanthrow_exception
=> booleanmessage
=> stringerrorCode
=> Check MA\LaravelApiResponse\Enums\ErrorCodesEnum (can be integer, string, or UnitEnum)status_code
=> integer (applies only if type is not sent)
Example with Error Code:
Response:
Validation Support
Note: The first parameter can be either Illuminate\Http\Request
or an array
Response for validation failure:
Pagination Support
Response:
Available Methods
Parameters:
- First parameter: paginated model
- Second parameter: whether to reverse the data or maintain original order
Error Handling Methods:
Parameters:
- First parameter: errors (string or array)
- Second parameter: whether to throw exception (default: true)
- Third parameter: error code (integer, string, null, or UnitEnum instance)
IMPORTANT: If error code is null, it will return the default error code if config returnDefaultErrorCodes
is true
Forbidden Error Response
Parameters:
- First parameter: message (string or null)
- Second parameter: errors (string, array, or null)
- Third parameter: error code (integer, string, null, or UnitEnum instance)
Default message is "Forbidden"
Note: If errors is null, the errors property won't appear in the response
Response:
Unauthenticated Error Response
Parameters:
- First parameter: message (string or null)
- Second parameter: errors (array, string, or null)
- Default message: "Unauthenticated"
Note: If errors is null, the errors property won't appear in the response
Response:
API Validation
Follows Laravel's validate() method pattern:
- First parameter: data
- Second parameter: roles
- Third parameter: messages
- Fourth parameter: custom attributes
Returns validated data on success or throws an exception using this trait on failure.
API Validation (Request)
Add this trait to your requests to handle validation errors.
Debug Helper
Response:
Error Codes Configuration
The package provides extensive error code configuration options:
- Enable/disable error codes
- Set error code enum class or custom enum class
- Set error codes output type (string or integer)
- Enable/disable returning default error codes if set as null
- Set error codes defaults for error functions
Publishing Error Codes Enum
Basic usage:
With custom class name:
If no custom name is specified, it will generate with the default class name "ErrorCodesEnum"
Contributors
Ahmed Elrayes |
Mohamed Aboushady |
License
The MIT License (MIT). Please see License File for more information.
All versions of lapi-response with dependencies
illuminate/auth Version >=10.43.0
illuminate/config Version >=10.43.0
illuminate/container Version >=10.43.0
illuminate/contracts Version >=10.43.0
illuminate/http Version >=10.43.0
illuminate/pagination Version >=10.43.0
illuminate/routing Version >=10.43.0
illuminate/support Version >=10.43.0
illuminate/validation Version >=10.43.0
symfony/http-foundation Version ^6.0 || ^7.0
psr/log Version ^3.0