jerrydepredator/laravel-predator-api-utils
Composer 安装命令:
composer require jerrydepredator/laravel-predator-api-utils
包简介
A collection of utility classes and traits to streamline common Laravel development tasks, including error handling, paginated responses, data handling, middleware creation, a base repository for advanced filtering, keyset pagination, base query filtering, and more. This package aims to reduce boile
README 文档
README
This package provides a collection of utility classes and traits to streamline common Laravel development tasks, such as:
- Error Handling: Easily handle and format API errors.
- Paginated Responses: Create paginated responses with consistent structure.
- Data Handling: Simplify data manipulation and extraction.
- Middleware: Implement custom middleware for authentication, authorization, and other cross-cutting concerns.
- Repository Pattern: Provides a base repository class for common database operations.
⚠️⚠️ Disclaimer ⚠️⚠️
This package aims to reduce boilerplate code often encountered in "MY LARAVEL PROJECTS" by providing reusable components. It was created based on my personal preferences and may not be suitable for all projects or development styles. It is not intended to be a comprehensive solution for all Laravel development needs and may require customization for specific requirements.
Installation
-
Install via Composer:
composer require jerrydepredator/laravel-predator-api-utils
-
Publish Configuration (Optional):
-
If you want to customize the default property names for the
DecryptedJWTokenservice, publish the configuration file:php artisan vendor:publish --provider="LaravelPredatorApiUtils\ServiceProvider" --tag="config"
-
This will create a
config/decrypted_jwt_token.phpfile in your project where you can modify the property names.
-
Usage
1. DecryptedJWTToken Service:
-
Location:
src/Services/DecryptedJWToken.php -
Purpose: Provides methods to access data from a decrypted JWT token.
-
Usage:
use LaravelPredatorApiUtils\Services\DecryptedJWToken; // In your controller public function showProfile(Request $request, DecryptedJWToken $decryptedJWToken) { $userId = $decryptedJWToken->getUserId(); $userName = $decryptedJWToken->getUserName(); $userData = $decryptedJWToken->getData(); // Get all user data // ... your logic using user data }
2. Middlewares:
-
DecryptJWToken:
- Location:
src/Middlewares/DecryptJWToken.php - Purpose: Decrypts the JWT token from the
Authorizationheader and adds the decrypted data to the request object. - Usage:
-
Register the middleware in
app/Http/Kernel.php:protected $routeMiddleware = [ // ... other middleware 'decrypt_jwt' => \App\Http\Middleware\DecryptJWToken::class, ];
-
Apply the middleware to routes:
Route::get('/protected-route', [MyController::class, 'showData'])->middleware('decrypt_jwt');
-
- Location:
-
RoleMiddleware:
- Location:
src/Middlewares/RoleMiddleware.php - Purpose: Restricts access to routes based on user roles.
- Usage:
-
Register the middleware in
app/Http/Kernel.php:protected $routeMiddleware = [ // ... other middleware 'role' => \App\Http\Middleware\RoleMiddleware::class, ];
-
Apply the middleware to routes:
Route::get('/admin', [AdminController::class, 'index'])->middleware('role:admin');
-
- Location:
3. Traits:
-
ApiResponse:
- Location:
src/Traits/ApiResponse.php - Purpose: Provides helper methods for creating API responses with standardized structures.
- Location:
-
HandlesErrors:
- Location:
src/Traits/HandlesErrors.php - Purpose: Provides helper methods for handling and formatting API errors.
- Location:
-
PaginationRules:
- Location:
src/Traits/PaginationRules.php - Purpose: Provides rules for validating pagination parameters.
- Location:
4. Repositories:
- BaseRepository:
- Location:
src/Repositories/BaseRepository.php - Purpose: Provides a base class for implementing repositories with common database operations.
- Location:
Configuration:
decrypted_jwt_token.php:- Located in the
configdirectory. - Defines default property names for the
DecryptedJWTokenservice. - Can be customized to match your specific JWT token structure.
- Located in the
Contributing
Contributions are welcome! Please submit pull requests to the project's GitHub repository.
License
This package is licensed under the MIT License.
jerrydepredator/laravel-predator-api-utils 适用场景与选型建议
jerrydepredator/laravel-predator-api-utils 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 93 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 12 月 23 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 jerrydepredator/laravel-predator-api-utils 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 jerrydepredator/laravel-predator-api-utils 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 93
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 20
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-12-23