Download the PHP package dev-toolbelt/jsend-payload without Composer
On this page you can find all versions of the php package dev-toolbelt/jsend-payload. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download dev-toolbelt/jsend-payload
More information about dev-toolbelt/jsend-payload
Files in dev-toolbelt/jsend-payload
Package jsend-payload
Short Description A framework-agnostic PHP library for building standardized JSend API responses with PSR-7 support
License MIT
Homepage https://github.com/Dev-Toolbelt/jsend-payload
Informations about the package jsend-payload
JSend Payload
A framework-agnostic PHP library for building standardized API responses following the JSend specification. Provides a simple trait that can be mixed into any controller or handler to generate consistent JSON responses with PSR-7 support.
Features
- Framework Independence - Works with Laravel, Symfony, Slim, Mezzio, or any PSR-7 compatible framework
- JSend Compliant - Follows the JSend specification for standardized API responses
- PSR-7 Support - Returns
Psr\Http\Message\ResponseInterfacefor maximum interoperability - Type-Safe HTTP Codes - Uses enums for HTTP status codes, preventing invalid values
- Common Response Helpers - Pre-built methods for validation errors, not found, empty payloads, and more
- Zero Configuration - Just use the trait and start returning standardized responses
Requirements
- PHP 8.1 or higher
Installation
Quick Start
Add the AnswerTrait to your controller and start returning standardized responses:
Understanding JSend
JSend is a specification for a simple, standardized format for JSON responses from web servers. It defines three response types:
| Status | Description | When to Use |
|---|---|---|
success |
All went well, data is returned | Successful GET, POST, PUT, DELETE operations |
fail |
Problem with submitted data or call conditions | Validation errors, missing required fields |
error |
Server error occurred | Exceptions, database failures, unexpected errors |
Usage
Success Responses
Use for successful operations that return data:
Fail Responses
Use when there's a problem with the data submitted:
Error Responses
Use for server-side errors:
No Content Response
Use when operation succeeds but there's no data to return:
Helper Methods
The trait includes convenient methods for common API scenarios:
| Method | HTTP Code | Description |
|---|---|---|
answerSuccess($data) |
200 | Successful response with data |
answerFail($data) |
400 | Client error with validation details |
answerError($message) |
500 | Server error with message |
answerNoContent() |
200 | Success with null data |
answerInvalidUuid() |
400 | Invalid UUID format error |
answerRecordNotFound() |
404 | Record not found error |
answerEmptyPayload() |
400 | Empty request body error |
answerRequired($field) |
400 | Required field missing error |
answerColumnNotFound($column) |
400 | Database column not found error |
Response Examples
answerInvalidUuid()
answerRecordNotFound()
answerRequired('email')
Framework Integration
Slim Framework
Laravel
Note: Laravel requires the
nyholm/psr7bridge or similar PSR-7 adapter for full compatibility.
Mezzio (formerly Zend Expressive)
Symfony
Note: Symfony requires the PSR-7 Bridge (
symfony/psr-http-message-bridge) for PSR-7 response compatibility.
Yii2
Note: For Yii2, you may need to configure your application to handle PSR-7 responses or convert them using
yiisoft/yii2-psr7-bridge.
CakePHP 5
Note: CakePHP 5 has native PSR-7 support. For CakePHP 4, the integration works similarly.
CodeIgniter 4
Note: CodeIgniter 4 supports PSR-7 through the
codeigniter4/psr7bridgepackage for full compatibility.
Testing
Security
If you discover any security-related issues, please email [email protected] instead of using the issue tracker.
Code Quality Requirements
| Check | Tool | Requirement |
|---|---|---|
| Tests | PHPUnit | All tests must pass |
| Coverage | PCOV | Minimum 85% coverage |
| Code Style | PHP CodeSniffer | PSR-12 compliant |
| Static Analysis | PHPStan | Level 6, no errors |
Pull Request Guidelines
Before submitting a PR, ensure:
- All tests pass:
composer test - Coverage is at least 85%:
composer test:coverage - Code style is correct:
composer phpcs - No static analysis errors:
composer phpstan
Important: Pull requests with coverage below 85% will be automatically blocked by CI.
Coverage Report
- Dashboard: Codecov
- HTML Report: GitHub Pages
License
MIT License. See LICENSE for details.