alirezajavadi/jsonize
最新稳定版本:1.8.1
Composer 安装命令:
composer require alirezajavadi/jsonize
包简介
Standardize JSON API responses with a fluent, chainable interface for PHP applications.
README 文档
README
A fluent PHP library for building standardized JSON API responses.
Requirements
- PHP 8.1 or higher
Installation
composer require alirezajavadi/jsonize
Quick Start
JSONize provides two usage modes: Easy (static, zero-boilerplate) and Efficient (singleton, memory-safe for long-running processes).
Easy Mode
use JSONize\App\Easy\Response; Response::message("User created")->status(201)->data(["id" => 42]);
Output:
{
"success": true,
"message": "User created",
"data": {"id": 42},
"status": [201, "Created"]
}
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:
$json = Response::message("OK")->status(200)->get();
Efficient Mode
use JSONize\App\Efficient\Response; $response = Response::getInstance(); $response->message("User created")->status(201)->data(["id" => 42])->get();
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.
Response::message("Operation successful");
status(int $code, string $message = "")
Sets the HTTP status code. Optionally pass a custom status message to override the default.
Response::status(404); Response::status(200, "Everything is fine");
data(mixed $data, string $key = "data")
Sets the response payload. The second argument changes the JSON key name.
Response::data(["id" => 1, "name" => "Item"]); Response::data(["id" => 1, "name" => "Item"], "item");
With a custom key, the output uses that key instead of "data":
{
"success": true,
"item": {"id": 1, "name": "Item"},
"status": [200, "OK"]
}
error(string $message, int $status = 500)
Shorthand for setting an error message and status code in one call.
Response::error("Something went wrong"); Response::error("Validation failed", 422);
headers(array $headers)
Merges custom HTTP headers into the response. CORS headers are included by default.
Response::message("OK")->headers(["X-Request-Id" => "abc-123"]);
hideStatus() / hideMessage() / hideData() / hideSuccess()
Remove specific fields from the JSON output.
Response::message("OK")->status(200)->hideStatus();
{
"success": true,
"message": "OK",
"data": null
}
get()
Returns the JSON string and prevents automatic output (Easy mode). In Efficient mode, this is the only way to get the response.
$json = Response::message("OK")->get();
Full Examples
Success with data:
Response::message("Users retrieved") ->status(200) ->data(["users" => [["id" => 1, "name" => "Alice"]]]);
{
"success": true,
"message": "Users retrieved",
"data": {"users": [{"id": 1, "name": "Alice"}]},
"status": [200, "OK"]
}
Validation error:
Response::error("Validation failed", 422) ->data(["email" => "The email field is required."], "errors");
{
"success": false,
"message": "Validation failed",
"errors": {"email": "The email field is required."},
"status": [422, "Unprocessable Content"]
}
Minimal response:
Response::status(204)->hideMessage()->hideData();
{
"success": true,
"status": [204, "No Content"]
}
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:
composer install vendor/bin/phpunit
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
alirezajavadi/jsonize 适用场景与选型建议
alirezajavadi/jsonize 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 72 次下载、GitHub Stars 达 39, 最近一次更新时间为 2024 年 05 月 09 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 alirezajavadi/jsonize 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 alirezajavadi/jsonize 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 72
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 39
- 点击次数: 12
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-05-09