Download the PHP package alirezajavadi/jsonize without Composer
On this page you can find all versions of the php package alirezajavadi/jsonize. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download alirezajavadi/jsonize
More information about alirezajavadi/jsonize
Files in alirezajavadi/jsonize
Package jsonize
Short Description JSONize: Standardize JSON responses effortlessly! Ensure consistency in your applications with our streamlined library. Simplify data formatting, enhance readability, and save time. JSONize empowers developers to generate clean, organized JSON outputs, perfect for APIs, web, and mobile apps. Revolutionize your workflow today!
License MIT
Informations about the package jsonize
JSONize
A fluent PHP library for building standardized JSON API responses.
Requirements
- PHP 8.1 or higher
Installation
Quick Start
JSONize provides two usage modes: Easy (static, zero-boilerplate) and Efficient (singleton, memory-safe for long-running processes).
Easy Mode
Output:
In Easy mode, the response is automatically sent when the object is destroyed. Call ->get() instead if you need the JSON string returned without echoing:
Efficient Mode
Efficient mode uses a singleton so you reuse the same instance. Always call ->get() to retrieve the JSON string.
API Reference
All methods are chainable and available in both modes.
message(string $message)
Sets the response message.
status(int $code, string $message = "")
Sets the HTTP status code. Optionally pass a custom status message to override the default.
data(mixed $data, string $key = "data")
Sets the response payload. The second argument changes the JSON key name.
With a custom key, the output uses that key instead of "data":
error(string $message, int $status = 500)
Shorthand for setting an error message and status code in one call.
headers(array $headers)
Merges custom HTTP headers into the response. CORS headers are included by default.
hideStatus() / hideMessage() / hideData() / hideSuccess()
Remove specific fields from the JSON output.
get()
Returns the JSON string and prevents automatic output (Easy mode). In Efficient mode, this is the only way to get the response.
Full Examples
Success with data:
Validation error:
Minimal response:
Supported HTTP Status Codes
All standard HTTP status codes (1xx through 5xx) are supported with their official reason phrases. Custom status messages can be provided via ->status($code, "Your message").
Testing
Tests run automatically via GitHub Actions on every push to main/develop and on pull requests, across PHP 8.1 through 8.4.
To run locally:
Contributing
Contributions are welcome. Please submit a pull request or open an issue for bugs and suggestions. Follow Conventional Commits for commit messages.
License
MIT — see LICENSE for details.
Author
Alireza Javadi