sardar-backend/lara-response
Composer 安装命令:
composer require sardar-backend/lara-response
包简介
A Laravel package for API response handling
README 文档
README
A robust and customizable API response helper for Laravel applications, designed to simplify JSON responses and maintain consistency across your API.
📌 Features
- Standardized API response structure
- Effortless HTTP status code management
- Append additional metadata seamlessly
- Fluent and intuitive interface
- Fully compatible with Laravel's response system
📦 Installation
Install the package via Composer:
composer require sardar-backend/lara-response:dev-master
🚀 Usage
1️⃣ Generate the Service Class
First, create a new service class using the following command:
php artisan make:service ModelName
To properly inject ProductServices into your controller, use the constructor method:
public function __construct(private ProductServices $productServices) { }
Basic Example
use SardarBackend\RestfulApiHelper\RestfulApi\Fecades\ApiResponseFacade; $result = $this->productServices->getAll($request->all()); if (!$result->ok) { return ApiResponseFacade::withMessage($result->data)->withStatus(500)->build()->Response(); } return ApiResponseFacade::withData(ApiProductListResource::collection($result->data)->resource)->build()->Response();
Adding Additional Data
use SardarBackend\RestfulApiHelper\RestfulApi\Fecades\ApiResponseFacade; $result = $this->productServices->getAll($request->all()); if (!$result->ok) { return ApiResponseFacade::withMessage($result->data)->withStatus(500)->build()->Response(); } return ApiResponseFacade::withData(ApiProductListResource::collection($result->data)->resource)->withAppends(['processing_time' => '2 seconds'])->build()->Response();
🛠 Methods
| Method | Description |
|---|---|
withMessage(string $message) |
Sets the response message |
withData(mixed $data) |
Sets the response data |
withStatus(int $status) |
Sets the HTTP status code |
withAppends(array $appends) |
Adds additional key-value pairs to the response |
response() |
Returns a Laravel JsonResponse object |
📜 License
This package is open-source and licensed under the MIT License.
🤝 Contributing
Contributions are welcome! Feel free to submit issues or pull requests on GitHub.
📬 Contact
For any inquiries, reach out via GitHub Issues.
统计信息
- 总下载量: 15
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: Unknown
- 更新时间: 2025-02-09