sevaske/laravel-api-response
最新稳定版本:1.0.0
Composer 安装命令:
composer require sevaske/laravel-api-response
包简介
The package for building clean, consistent, and predictable JSON API responses in laravel applications.
README 文档
README
A simple library for a simple task: building consistent JSON API responses in Laravel. Fully customizable when you need it
What this package is
- a small abstraction over JSON responses
- a way to standardize API responses across your app
- minimal by default, customizable by design
- IDE-friendly
Default response format
Out of the box, the response looks like this:
{
"success": true,
"message": "OK",
"data": {
"id": 1
}
}
Error response:
{
"success": false,
"message": "Validation failed",
"errors": {
"email": "Invalid"
}
}
All keys and values are fully configurable via config or by replacing the payload builder.
Installation
composer require sevaske/laravel-api-response
Optional config publishing:
php artisan vendor:publish --tag=api-response-config
Usage
1. Dependency Injection (recommended)
use Sevaske\LaravelApiResponse\Contracts\ApiResponseContract; class UserController { public function __construct( private ApiResponseContract $api ) {} public function index() { return $this->api->success('OK', [ 'id' => 1, ]); } }
2. Via response() macros
return response()->success( message: 'OK', data: ['id' => 1], ); return response()->error( message: 'Validation failed', errors: ['email' => 'Invalid'] );
3. Via helper
return api()->success( message: 'OK', data: ['id' => 1], );
Customization
Change response keys:
return [ 'success_key' => 'ok', 'message_key' => 'msg', 'data_key' => 'results', 'errors_key' => 'errors', ];
Change the "success" value format:
return [ 'success_value' => 1, 'error_value' => 0, ];
Extending
Bind your own response implementation
use Sevaske\LaravelApiResponse\Contracts\ApiResponseContract; $this->app->bind(ApiResponseContract::class, MyCustomApiResponse::class);
Replace the payload builder
use Sevaske\ApiResponsePayload\Contracts\ApiResponsePayloadContract; $this->app->bind(ApiResponsePayloadContract::class, MyPayloadBuilder::class);
This allows full control over the final response structure without touching controllers.
License
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-01-05