Download the PHP package miladev/api-response without Composer
On this page you can find all versions of the php package miladev/api-response. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download miladev/api-response
More information about miladev/api-response
Files in miladev/api-response
Package api-response
Short Description A simple Laravel package for success and fail response traits.
License MIT
Homepage https://github.com/miladev95/api-response
Informations about the package api-response
Laravel Api Response
A small, framework-friendly trait that standardizes JSON API responses (success and error). The trait is lightweight and designed to be easy to override in consuming applications.
Features
- Standardized JSON success and error responses
- Small and framework-agnostic: prefers framework response helpers when available
- Extensible: override payload shape or header handling
Requirements
- PHP >= 8.1
- ext-json
Installation
Install via Composer:
Quick usage (Laravel)
Overriding payload structure
If you want a different JSON structure (for example to include meta or to follow a specification), override formatSuccessPayload or formatErrorPayload in your class. Below is a fuller example showing a custom success payload and preserving type hints.
This keeps the trait's public API (successResponse) the same while changing only the response shape.
Overriding headers
If you need to inject or normalize headers (for example, pagination headers), override prepareHeaders. The example below calls the parent to keep the default Content-Type normalization and then adds pagination headers.
Note: prepareHeaders normalizes header values and ensures Content-Type: application/json if not provided.
Non-Laravel / Testing
The trait prefers a response() factory when available (Laravel). For testing or non-framework usage you can provide a small response helper. The test bootstrap included in the project demonstrates this approach (it provides a minimal response() helper and a TestResponse object). That keeps unit tests fast and framework-independent.
API reference
Public methods
-
successResponse($data = [], string $message = '', int $statusCode = 200, array $headers = [])- Returns a standardized success JSON response. You can pass any payload as
$data.
- Returns a standardized success JSON response. You can pass any payload as
failResponse(string $message = '', int $statusCode = 400, array $headers = [])- Returns a standardized error JSON response.
Protected / overridable helpers
formatSuccessPayload($data, string $message): array— Customize success payload shape.formatErrorPayload(string $message): array— Customize error payload shape.prepareHeaders(array $headers): array— Normalize and add default headers.createResponse($payload, int $statusCode, array $headers)— Central response factory (you can override to integrate with custom response objects).
Testing locally
To run tests locally (from project root):
The tests use tests/bootstrap.php which provides a minimal response() helper so tests can run without Laravel.
All versions of api-response with dependencies
ext-json Version *