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.
Macros
You can register custom response helpers at runtime via macro(). Macros are scoped to the class that uses the trait and are invoked with $this bound to the instance (or to the first argument when called statically).
ApiResponse::hasMacro('name') returns whether a macro is registered. Calling an unregistered macro throws BadMethodCallException.
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.
Roadmap
Planned features and improvements for upcoming releases:
- Pagination helper — first-class
paginatedResponse($paginator)that auto-includesmeta(current page, per page, total) and pagination headers (X-Total-Count,Link). - Built-in exception handler — a
reportableExceptions()helper that maps common exceptions (ValidationException, ModelNotFoundException, AuthenticationException) to standard error responses. - Laravel package auto-discovery — add
extra.laravel.providerstocomposer.jsonso Laravel auto-registers the trait and helpers. - Response caching helper —
cachedResponse($key, $ttl, $callback)for ETag/Last-Modified style caching wrappers. - Locale-aware messages — support translating
$messagearguments via Laravel'strans()helper, with a fallback when used outside Laravel. - Convenience shortcuts —
ok(),created(),noContent(),unauthorized(),forbidden(),notFound(),validationError()covering common HTTP status codes. - OpenAPI / JSON:API shape presets — opt-in payload shapes that follow popular API specifications.
- Streaming / SSE response helper —
streamResponse($generator)for server-sent events and large dataset responses. - PHPStan + GitHub Actions quality gates — add static analysis (level 6+) and code style checks (PHP-CS-Fixer / Pint) to CI alongside the existing PHPUnit run.
All versions of api-response with dependencies
ext-json Version *