Download the PHP package ayd/api-response-laravel without Composer
On this page you can find all versions of the php package ayd/api-response-laravel. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download ayd/api-response-laravel
More information about ayd/api-response-laravel
Files in ayd/api-response-laravel
Package api-response-laravel
Short Description An API response package for Laravel applications at AYD Company.
License MIT
Informations about the package api-response-laravel
API Response Laravel
Laravel integration for the ayd/api-response-base package. Provides a service class, trait, facade, and global helper functions for building consistent JSON API responses.
Installation
The service provider is auto-discovered. To register manually:
Usage
1. Dependency Injection
Inject ApiResponse directly into your controller or service:
2. Trait (for Controllers)
Use ApiResponseTrait to call response methods directly on the controller:
3. Facade
To use the facade, add it to config/app.php:
4. Global Helper Functions
Helper functions are auto-loaded and return JsonResponse directly. Error helpers return the response (they do not throw exceptions):
Error Handling
Error methods on the service class and trait throw HttpResponseException rather than returning a value. This means you can call them without return:
Available error methods: fail(), error(), badRequest(), unauthorized(), forbidden(), notFound(), unprocessable().
Paginated Responses
Transform paginated results with a Laravel API Resource class:
This automatically:
- Transforms items via
UserResource::collection($paginator->items())->resolve(request()) - Extracts pagination metadata (
total,per_page,current_page) intometa.pagination - Includes
request_idand any resolved abilities inmeta
Abilities Resolver
Bind your resolver in a service provider to auto-inject abilities into every response:
The ResponseServiceProvider checks if AbilitiesResolver is bound and passes it to ApiResponse automatically.
API Reference
Success Methods
| Method | HTTP Status | Return Type |
|---|---|---|
success($data, $message, $meta) |
200 | JsonResponse |
created($data, $message, $meta) |
201 | JsonResponse |
updated($data, $message, $meta) |
200 | JsonResponse |
accepted($data, $message, $meta) |
202 | JsonResponse |
noContent() |
204 | JsonResponse |
paginator($paginator, $resourceClass, $meta) |
200 | JsonResponse |
Error Methods
| Method | HTTP Status | Return Type |
|---|---|---|
fail($code, $message, $data, $meta) |
configurable | void (throws) |
error($code, $message, $data, $meta) |
configurable | void (throws) |
badRequest($message, $data) |
400 | void (throws) |
unauthorized($message) |
401 | void (throws) |
forbidden($message) |
403 | void (throws) |
notFound($message) |
404 | void (throws) |
unprocessable($message, $data) |
422 | void (throws) |
Requirements
- PHP ^8.1
ayd/api-response-base^1.0illuminate/http^10.0 | ^11.0 | ^12.0illuminate/support^10.0 | ^11.0 | ^12.0
License
MIT
All versions of api-response-laravel with dependencies
ayd/api-response-base Version ^1.0
illuminate/http Version ^10.0|^11.0|^12.0|^13.0
illuminate/support Version ^10.0|^11.0|^12.0|^13.0