Download the PHP package philiprehberger/php-api-response without Composer
On this page you can find all versions of the php package philiprehberger/php-api-response. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Table of contents
Download philiprehberger/php-api-response
More information about philiprehberger/php-api-response
Files in philiprehberger/php-api-response
Download philiprehberger/php-api-response
More information about philiprehberger/php-api-response
Files in philiprehberger/php-api-response
Vendor philiprehberger
Package php-api-response
Short Description Standardized API response builder for consistent JSON APIs
License MIT
Homepage https://github.com/philiprehberger/php-api-response
Package php-api-response
Short Description Standardized API response builder for consistent JSON APIs
License MIT
Homepage https://github.com/philiprehberger/php-api-response
Please rate this library. Is it a good library?
Informations about the package php-api-response
PHP API Response
Standardized API response builder for consistent JSON APIs.
Requirements
- PHP 8.2+
Installation
Usage
Success Responses
Error Responses
Paginated Responses
Serialization
ResponsePayload implements JsonSerializable and Stringable:
Using with Laravel
Return responses directly from controllers by accessing the payload properties:
Fluent Chaining
All with* methods return a new ResponsePayload instance, keeping the original unchanged:
Response Shape
All responses follow a consistent structure:
success(bool) - Always presentmessage(string) - Always presentdata(mixed) - Always presenterrors(object) - Only present when there are errorsmeta(object) - Only present when metadata is provided (e.g., pagination)
API
| Method | Status Code | Description |
|---|---|---|
ApiResponse::success($data, $message) |
200 | Successful response with optional data |
ApiResponse::created($data, $message) |
201 | Resource created successfully |
ApiResponse::noContent($message) |
204 | Success with no response body |
ApiResponse::error($message, $statusCode, $errors) |
400 | Generic error response |
ApiResponse::validationError($errors, $message) |
422 | Validation failure with field errors |
ApiResponse::notFound($message) |
404 | Resource not found |
ApiResponse::unauthorized($message, $errors) |
401 | Authentication required or failed |
ApiResponse::forbidden($message, $errors) |
403 | Authenticated but not permitted |
ApiResponse::accepted($data, $message) |
202 | Request accepted for asynchronous processing |
ApiResponse::internalServerError($message, $errors) |
500 | Unexpected server-side failure |
ApiResponse::paginated($items, $total, $page, $perPage) |
200 | Paginated list with metadata |
Fluent Methods on ResponsePayload
| Method | Description |
|---|---|
withMeta(array $meta) |
Returns a new instance with merged metadata |
withHeaders(array $headers) |
Returns a new instance with custom response headers |
withStatusCode(int $code) |
Returns a new instance with overridden HTTP status code |
withPagination(int $total, int $page, int $perPage) |
Returns a new instance with a pagination block merged into meta |
Development
Support
If you find this project useful:
License
MIT
All versions of php-api-response with dependencies
PHP Build Version
Package Version
Requires
php Version
^8.2
The package philiprehberger/php-api-response contains the following files
Loading the files please wait ...