ferdous-ahmed-khan/laravel-api-response
Composer 安装命令:
composer require ferdous-ahmed-khan/laravel-api-response
包简介
A Laravel package providing standardized JSON response helpers for success and error handling, with functions for quick, consistent API responses.
README 文档
README
This guide will walk you through the installation and setup process for integrating the package into your Laravel project.
JSON Response Structure
{
"status": true|false,
"message": "Your message here",
"data": { /*optional data */ },
"errors": { /* optional error details*/ }
}
Requirements
- Laravel 8.x or higher
- PHP 7.4+ (or higher, depending on the package requirements)
- Composer
Installation
To install the package, follow these steps:
1. Install via Composer
Run the following command in your Laravel project root directory to install the package:
composer require ferdous-ahmed-khan/laravel-api-response
2. Open config/app.php
In the providers array, add the following line to register the service provider:
'providers' => [ // Other Service Providers... FerdousAhmedKhan\LaravelApiResponse\ResponseHelperServiceProvider::class, ],
In the aliases array, add the following line to create an alias for the ResponseHelper facade:
'aliases' => [ // Other Aliases... 'ApiResponse' => FerdousAhmedKhan\LaravelApiResponse\ResponseHelperFacade::class, ],
3. Usage
Once the package is installed, you can start using its features. For example, to use the
use ApiResponse;
success()
Generates a successful response with HTTP status 200.
return ApiResponse::success('Operation successful', $data);
deleted()
Generates a response for successful deletions with HTTP status 200
return ApiResponse::deleted('Record deleted successfully', $data);
created()
Generates a response for successful creation with HTTP status 201
return ApiResponse::created('Resource created successfully', $data);
badRequest()
Generates a response for a bad request with HTTP status 400.
return ApiResponse::badRequest('Invalid request data', $errorDetails);
failed()
Generates a generic failure response with HTTP status 400.
return ApiResponse::failed('Operation failed', $errorDetails);
unauthorized()
Generates a response for forbidden access with HTTP status 403.
return ApiResponse::forbidden('Access is forbidden', $errorDetails);
forbidden()
Generates a response for forbidden access with HTTP status 403.
return ApiResponse::forbidden('Access is forbidden', $errorDetails);
notFound()
Generates a response for resources not found with HTTP status 404.
return ApiResponse::notFound('Resource not found', $errorDetails);
validationFailed()
Generates a response for validation failure with HTTP status 422.
return ApiResponse::validationFailed('Validation failed', $validationErrors);
internalServerError()
Generates a response for server errors with HTTP status 500.
return ApiResponse::internalServerError('An error occurred', $errorDetails);
custom()
Generates a custom response with a specified HTTP status code.
return ApiResponse::custom(true, 202, 'Request accepted', $data, null);
Each function returns a JsonResponse object with a consistent structure:
{
"status": true|false,
"message": "Your message here",
"data": { /*optional data */ },
"errors": { /* optional error details*/ }
}
ferdous-ahmed-khan/laravel-api-response 适用场景与选型建议
ferdous-ahmed-khan/laravel-api-response 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 15 次下载、GitHub Stars 达 1, 最近一次更新时间为 2024 年 11 月 08 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 ferdous-ahmed-khan/laravel-api-response 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 ferdous-ahmed-khan/laravel-api-response 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 15
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 14
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-11-08