Download the PHP package haseebmirza/laravel-api-response without Composer
On this page you can find all versions of the php package haseebmirza/laravel-api-response. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package laravel-api-response
Laravel API Response
A lightweight, zero-config package that gives your Laravel API a consistent JSON response format — so every endpoint speaks the same language.
Why?
Without a standard response helper, API responses across your app end up looking different — some return status, others return success, error formats vary, and pagination metadata is inconsistent. This package solves that with a single static class and an optional trait.
Before:
After:
Every response follows the same structure. Every time.
Requirements
- PHP 8.1+
- Laravel 10, 11, 12, or 13
Installation
The service provider is auto-discovered — no manual registration needed.
Quick Start
Example JSON response:
Usage
Choose The Right Place For Extra Data
Use each part of the response for a specific purpose:
data: the main business payload such as user, order, token payload, or collectionmeta: structured metadata related to the response such as pagination, API version, filters, or source- custom top-level fields: optional extra fields such as
request_id,debug,trace_id,links, or anything your frontend/client needs outsidemeta
Example:
Static Class
Use ApiResponse directly anywhere in your application:
Common Real-World Snippets
1. Return a single resource
2. Return a newly created resource
3. Return validation errors
4. Return custom top-level fields
5. Return an API token after login
6. Return an empty 204 response
noContent() returns HTTP 204 with an empty response body. That matches HTTP semantics and avoids clients discarding a JSON payload.
Pagination
Wrap Laravel's paginator to include pagination metadata automatically:
Response:
You can also pass extra metadata:
And if you need custom top-level fields outside meta:
Authentication / Token Response
Return a token with optional user data — works with Sanctum, JWT, or any token-based auth:
Response:
Controller Trait
Prefer $this-> syntax? Add the ApiResponder trait to your controller or a base controller:
Then use it in any controller:
Trait example with extra fields:
Response Format
All responses follow a consistent JSON structure:
Success:
Success with metadata:
Success with custom top-level fields:
No content:
HTTP 204 with an empty body.
Error:
Error with details:
API Reference
Static Methods (ApiResponse)
| Method | Status | Description |
|---|---|---|
success($data, $message, $statusCode, $meta, $additional) |
200 | General success |
created($data, $message, $meta, $additional) |
201 | Resource created |
noContent($message, $additional) |
204 | No content, empty non-JSON body |
error($message, $statusCode, $errors, $additional) |
400 | General error |
unauthorized($message, $additional) |
401 | Authentication required |
forbidden($message, $additional) |
403 | Access denied |
notFound($message, $additional) |
404 | Resource not found |
validationError($errors, $message, $additional) |
422 | Validation failed |
serverError($message, $additional) |
500 | Internal server error |
paginated($paginator, $message, $meta, $additional) |
200 | Paginated collection |
withToken($token, $user, $tokenType, $message, $meta, $additional) |
200 | Auth token response |
Reserved keys:
successmessagedatametaerrors
If a developer passes any of these inside $additional, the package ignores them so the response contract stays stable.
Trait Methods (ApiResponder)
| Trait Method | Calls |
|---|---|
$this->successResponse(...) |
ApiResponse::success() |
$this->errorResponse(...) |
ApiResponse::error() |
$this->createdResponse(...) |
ApiResponse::created() |
$this->noContentResponse(...) |
ApiResponse::noContent() |
$this->notFoundResponse(...) |
ApiResponse::notFound() |
$this->unauthorizedResponse(...) |
ApiResponse::unauthorized() |
$this->forbiddenResponse(...) |
ApiResponse::forbidden() |
$this->validationErrorResponse(...) |
ApiResponse::validationError() |
$this->serverErrorResponse(...) |
ApiResponse::serverError() |
$this->paginatedResponse(...) |
ApiResponse::paginated() |
$this->tokenResponse(...) |
ApiResponse::withToken() |
Testing
If your machine has multiple PHP versions installed, make sure you run tests with a supported version for your current dependency lock file. Example:
Recommended pre-publish check:
Then install the package in a fresh Laravel app and verify a real route:
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
License
The MIT License (MIT). Please see LICENSE for more information.