iqbalatma/laravel-utils
Composer 安装命令:
composer require iqbalatma/laravel-utils
包简介
README 文档
README
Laravel utils is collection of class, helper, command console for help you to efficiency your development time. Here we have command console for generate enum, abstract, interface, or trait
Config
Target dir is where generated file root path. Change this target dir location by your preference.
return [ "target_enum_dir" => "app/Enums", "target_trait_dir" => "app/Traits", "target_abstract_dir" => "app/Contracts/Abstracts", "target_interface_dir" => "app/Contracts/Interfaces", "api_response" => [ "payload_wrapper" => "payload", "meta_wrapper" => null ], "is_show_debug" => env("APP_DEBUG", false) ];
Generate File Command
You can generate enum with this utils You can also create backed enum, with string or int as type
Enum
php artisan make:enum Gender php artisan make:enum Gender --type=string
You can generate trait with this command
Trait
php artisan make:trait HasInstitution
You can generate abstract with this command
Abstract
php artisan make:abstract BaseService
You can generate interface with this command
Interface
php artisan make:inteface IRouter
Publish Stub File
In some condition you may publish stub file and modify template.
php artisan utils:publish-stub
Formatting API Response
You can format your api response using this feature. For example :
<?php namespace App\Http\Controllers\API\Internal\Management; use App\Http\APIResponse; use App\Http\Controllers\ApiController; use App\Models\Permission; use App\Http\Resources\Internal\Management\Permissions\PermissionResourceCollection; class PermissionController extends ApiController { /** * @return APIResponse */ public function index(): APIResponse { $data = Permission::all(); return $this->response( new PermissionResourceCollection($data), "Get all data permission successfully" ); } }
Description :
- First argument is for data, you can pass paginator, string, array, resource, and null.
- Second argument is for message, data type is string
- Third argument is response code. You can see response code in Iqbalatma/LaravelUtils/ResponseCode
- Forth argument is errors, mostly used for validation errors.
- Fifth argument is for exception, mostly used for mapping execption.
The response would be
{
"rc": "SUCCESS",
"message": "Get all data permission successfully",
"timestamp": "2023-10-26T23:49:47.387526Z",
"payload": {
"data": [
{
"id": "99ffccb6-b375-4cf5-9f4c-b6824fabeab3",
"name": "can show all user",
"guard_name": "api"
},
{
"id": "99ffccb6-b375-4cf5-9f4c-b6824fabeab4",
"name": "can show all products",
"guard_name": "api"
}
]
}
}
You can customize response code (rc) using third argument. You can override to add response code of this class. Just override the class and override mapHttpCode() function. For example you can add response code for ERR_NOT_FOUND()
<?php namespace App\Services\V1; use Iqbalatma\LaravelUtils\Interfaces\ResponseCodeInterface; use Symfony\Component\HttpFoundation\Response; /** * @method static ResponseCodeInterface ERR_NOT_FOUND() */ class ResponseCode extends \Iqbalatma\LaravelUtils\ResponseCode { protected const ERR_NOT_FOUND = "ERR_NOT_FOUND"; /** * @return void */ protected function mapHttpCode(): void { $this->httpCode = match ($this->name) { self::ERR_NOT_FOUND => Response::HTTP_NOT_FOUND, default => null }; if ($this->httpCode === null) { parent::mapHttpCode(); } } }
This is how to handle exception and customize response format:
$this->renderable(function (NotFoundHttpException $e) { if (request()->expectsJson()) { return new APIResponse( null, message: $e->getMessage(), responseCode: ResponseCode::ERR_NOT_FOUND(), exception: $e ); } });
You can change behavior of json formatting via config file.
... "api_response" => [ "payload_wrapper" => "payload", "meta_wrapper" => "meta" ], "is_show_debug" => env("APP_DEBUG", false) ...
- Key is_show_debug use to determine is show exception message or not. This might be cause sensitive information leaks, so do not activate this feature in production environtmen.
- Key api_response.payload used for key of response. You can change wrapper with another wrapper or set null to not using payload wrapper.
- Key api_response.meta_wrapper is used for wrapping data meta. When you using
Helper
ddapi()
You can use ddapi() helper function to dump api data to see data of a variable
ddapi($data); ddapi($data, $data2);
iqbalatma/laravel-utils 适用场景与选型建议
iqbalatma/laravel-utils 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.98k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 10 月 16 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 iqbalatma/laravel-utils 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 iqbalatma/laravel-utils 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 1.98k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 4
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: Unknown
- 更新时间: 2023-10-16