定制 maatify/api-response 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

maatify/api-response

Composer 安装命令:

composer require maatify/api-response

包简介

Slim-compatible JSON API response handler for validation, errors, and success messages.

README 文档

README

Maatify.dev

Current version PHP Version Support Monthly Downloads Total Downloads Stars Tests

Maatify API Response is a lightweight, PSR-7 compatible, Slim-friendly structured JSON response library for clean and consistent API responses. Built with production logging in mind via maatify/slim-logger. It is part of the modular Maatify.dev ecosystem.

🚀 Features

  • ✅ PSR-7 compatible (ResponseInterface)
  • ✅ Slim-ready (return Json::...)
  • ✅ Structured error responses (missing, invalid, etc.)
  • ✅ Success responses with metadata
  • ✅ Automatic route+line trace: user:login::55
  • ✅ Production-safe JSON POST logging via maatify/slim-logger
  • ✅ Environment toggles for logging
  • ✅ Optional JsonResponseEmitter for non-framework use

🛠 Installation

composer require maatify/api-response

Requires PHP 8.1+
Uses maatify/slim-logger under the hood for logging (installed automatically)

📦 Usage in Slim

use Maatify\ApiResponse\Json;

$app->post('/register', function ($request, $response) {
    return Json::missing($response, 'email');
});

💡 Usage in Pure PHP (no framework)

Use the built-in JsonResponseEmitter class to send PSR-7 responses directly in native PHP.

require 'vendor/autoload.php';

use Maatify\ApiResponse\Json;
use Maatify\ApiResponse\JsonResponseEmitter;
use Nyholm\Psr7\Response;

$response = new Response();

if (empty($_POST['email'])) {
    $response = Json::missing($response, 'email', 'Email is required', __LINE__);
}

JsonResponseEmitter::emit($response);

🔁 Output

HTTP/1.1 400 Bad Request
Content-Type: application/json
{
  "success": false,
  "response": 1000,
  "var": "email",
  "description": "MISSING Email",
  "more_info": "Email is required",
  "error_details": "index::15"
}

✨ Example Slim Response

{
  "success": false,
  "response": 1000,
  "var": "email",
  "description": "MISSING Email",
  "more_info": "",
  "error_details": "register::34"
}

✅ Supported Methods

Method Description
missing() Field missing from input
incorrect() Incorrect format or value
invalid() Invalid input
notExist() Value doesn't exist (e.g. user ID)
success() Standard success output
dbError() Internal DB/system error
tooManyAttempts() Rate limit exceeded

📊 HTTP Status Code Reference

Method Status
missing() 400
incorrect() 400
invalid() 400
notExist() 400
success() 200
dbError() 500
tooManyAttempts() 429

🔐 Secure Logging

Enable structured logging in production via maatify/slim-logger

1. Enable in your .env

JSON_POST_LOG=1

2. Sample Logged Output

{
  "response": {
    "success": false,
    "response": 1000
  },
  "posted_data": {
    "email": "user@test.com",
    "password": "******"
  }
}

Log file: logs/api/response.log
Log level: Info

📂 Directory Structure

src/
├── CoreResponder.php        # Base logic + logger
├── BaseResponder.php        # Validation & error helpers
└── Json.php                 # Final static entrypoint class
└── JsonResponseEmitter.php  # Sends PSR-7 responses in native PHP

🧩 Extend It

Want custom codes or more logic?

  • Extend BaseResponder or CoreResponder
  • Override any method (e.g. add audit log or rate limiter)
  • Customize logResponse() for deeper monitoring

🧪 Testing

✅ Run Tests

composer test

✅ Run One Test

composer test -- --filter testSuccessResponse

✅ CI (GitHub Actions)

Tested on PHP 8.2, 8.3, and 8.4 using run-tests.yml

🆚 Slim vs Pure PHP Comparison

Feature Slim Pure PHP
Routing $app->post(... Native index.php or custom
JSON Response return Json::success(...) $response = Json::success(...)
Headers/Status Handled by Slim You manually set headers + status
Logging Via maatify/slim-logger ✅ Works the same

📄 License

MIT License © 2025 Maatify.dev

🙋‍♂️ Questions or Feedback?

maatify/api-response 适用场景与选型建议

maatify/api-response 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 04 月 18 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「json」 「api」 「response」 「maatify」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 maatify/api-response 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 maatify/api-response 我们能提供哪些服务?
定制开发 / 二次开发

基于 maatify/api-response 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 1
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 22
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-04-18