定制 bhry98/laravel-api-response 二次开发

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

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

bhry98/laravel-api-response

最新稳定版本:v1.0.0

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-10-26