bhry98/laravel-api-response
Composer 安装命令:
composer require bhry98/laravel-api-response
包简介
laravel package for handle responses for api projects
README 文档
README
A fluent, extensible, and developer-friendly API response builder for Laravel applications.
🚀 Features
- ✨ Fluent Interface — Chain methods to build responses easily.
- 🧩 Response Types — Supports
success,error,validation,not found, and more. - ⚙️ Customizable Messages — Override defaults or localize them.
- 🧱 Macroable Support — Extend response types dynamically.
- 🧮 Meta Data Support — Add pagination or extra info with
meta(). - 🌍 Localization Ready — Pull response messages from Laravel translations.
- 🔍 Trace Support — Optionally include exception traces for debugging.
📦 Installation
composer require bhry98/laravel-api-response
⚙️ Configuration
Publish the configuration file:
php artisan vendor:publish --provider="Bhry98\LaravelApiResponse\Providers\LaravelApiResponseServiceProvider" --tag=config
This will create config/bhry98-api-response.php where you can customize default messages:
return [ 'messages' => [ 'success' => 'Operation successful', 'error' => 'An error occurred', 'validation' => 'Validation failed', 'not_found' => 'Resource not found', 'internal' => 'Internal server error', ], ];
🧠 Usage Example
use Bhry98\LaravelApiResponse\Facades\BResponseSuccess; use Bhry98\LaravelApiResponse\Facades\BResponseError; Route::get('/{t}', function ($t) { return match ($t) { "success" => BResponseSuccess::make() ->message('Everything is fine!') ->data(['id' => 1]) ->additions(['request_id' => uniqid()]) ->toJson(), default => BResponseError::make() ->message('Something went wrong!') ->trace(new \Exception('Example')) ->toJson(), }; });
🧩 Example Response
✅ Success
{
"status": "success",
"message": "Everything is fine!",
"data": { "id": 1 },
"additions": { "request_id": "654adf..." },
"meta": []
}
❌ Error
{
"status": "error",
"message": "Something went wrong!",
"trace": "Exception stack trace (optional)",
"data": [],
"additions": [],
"meta": []
}
🧱 Extend Response (Macroable)
BResponseSuccess::macro('withTimestamp', function () { return $this->additions(['timestamp' => now()]); }); // Usage return BResponseSuccess::make() ->withTimestamp() ->toJson();
🧩 Available Methods
| Method | Description |
|---|---|
make($options = []) |
Initialize response |
message($message) |
Set response message |
data($data) |
Attach data payload |
additions(array $extras) |
Add custom key/value pairs |
meta($info) |
Add pagination/meta info |
trace($exception) |
Include exception trace |
localize($key) |
Use translation key for message |
toJson() |
Return Illuminate\Http\JsonResponse |
⚖️ License
This package is open-sourced software licensed under the MIT license.
Developed with ❤️ by Bhry98
bhry98/laravel-api-response 适用场景与选型建议
bhry98/laravel-api-response 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 632 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 10 月 26 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 bhry98/laravel-api-response 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 bhry98/laravel-api-response 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 632
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 12
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-10-26