elyerr/api-response
Composer 安装命令:
composer require elyerr/api-response
包简介
Add news functionalities for oauth2 passport server
README 文档
README
API Response is a Laravel package that provides a collection of reusable tools commonly required when building APIs and backend services.
Instead of repeatedly implementing the same helpers, validation rules, transformers, response formatting, and utility functions across multiple projects, API Response centralizes them into a single package designed to improve consistency, maintainability, and developer productivity.
The package is actively used throughout the ElyerrLabs ecosystem, including OAuth2 Passport Server and related services.
Features
JSON API Responses
Provides a standardized way to return API responses using the included JsonResponser trait.
Benefits:
- Consistent API responses
- Standardized success and error formats
- Cleaner controllers
- Better frontend integration
- Reduced boilerplate code
Example:
return $this->showOne($user);
return $this->showAll($users);
return $this->errorResponse(
'Resource not found',
404
);
Custom Validation Rules
API Response includes reusable validation rules for common scenarios.
BooleanRule
Converts and validates boolean values received from forms, APIs, and external services.
Supported examples:
true
false
1
0
MoneyRule
Validates monetary values and converts them into integer-based storage formats.
Example:
10.50
Stored as:
1050
This follows the same approach used by payment providers such as Stripe, helping prevent floating-point precision issues.
StringOnlyRule
Ensures a value contains only valid string content.
Useful for:
- Names
- Labels
- Titles
- Categories
UndefinedValues
Allows validation against values considered empty, undefined, or invalid for your application.
Useful when consuming data from external APIs or user-generated payloads.
Transformers
Generate transformers using:
php artisan api-response:transformer UserTransformer
Transformers help separate data presentation from business logic and create consistent API responses.
Example:
class UserTransformer extends Transformer
{
public function transform(User $user)
{
return [
'id' => (int) $user->id,
'name' => $user->name,
];
}
}
Helper Functions
The package includes several utility helpers frequently required in Laravel applications.
Examples include:
- Temporary password generation
- Unique code generation
- Date formatting
- Money formatting
- Array transformations
- Request flattening
- Slug generation
- Kebab-case conversion
- Time range validation
- File manipulation helpers
Example:
money_to_cents('10.50');
Result:
1050
Asset Utilities
The package provides reusable utility methods through the Asset helpers.
Common use cases:
- Password generation
- Unique identifiers
- Date formatting
- Array processing
- File operations
- String manipulation
These utilities are designed to eliminate repetitive code commonly found across Laravel projects.
Exception Handling
Includes utilities for standardizing exception reporting and API error responses.
This helps maintain consistent error handling throughout your application.
Installation
Stable Version
composer require elyerr/api-response
Development Version
composer require elyerr/api-response dev-main
Artisan Commands
Install Package Resources
Publish the default resources used by the package.
php artisan api-response:install
Create a Transformer
Generate a new transformer using the package stub.
php artisan api-response:transformer UserTransformer
Why Use API Response?
Most Laravel applications end up implementing the same functionality repeatedly:
- API response formatting
- Validation rules
- Transformers
- Utility helpers
- Date handling
- Money handling
- Data normalization
- Exception formatting
API Response centralizes these concerns into a single package, allowing developers to focus on business logic instead of rewriting infrastructure code.
Extensible by Design
API Response is intentionally lightweight and extensible.
You can:
- Create your own validation rules
- Extend transformers
- Add custom helpers
- Customize response formats
- Integrate package features into your own architecture
The package is designed to adapt to your workflow rather than forcing a specific development style.
License
AGPL-3.0
elyerr/api-response 适用场景与选型建议
elyerr/api-response 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.68k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 10 月 15 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 elyerr/api-response 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 elyerr/api-response 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 1.68k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 12
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: AGPL-3.0-only
- 更新时间: 2023-10-15