Download the PHP package tomeet/laravel-enum without Composer
On this page you can find all versions of the php package tomeet/laravel-enum. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download tomeet/laravel-enum
More information about tomeet/laravel-enum
Files in tomeet/laravel-enum
Package laravel-enum
Short Description A simple and easy-to-use enumeration extension package to help you manage enumerations in your project more conveniently, supporting Laravel and Lumen
License MIT
Informations about the package laravel-enum
laravel-enum
A simple and easy-to-use enumeration extension package to help you manage enumerations in your project more conveniently, supporting Laravel and Lumen.
- 一个简单好用的枚举扩展包,帮助你更方便地管理项目中的枚举,支持 Laravel 和 Lumen。
A simple and easy-to-use enumeration extension package to help you manage enumerations in your project more conveniently, supporting Laravel and Lumen.
- 一个简单好用的枚举扩展包,帮助你更方便地管理项目中的枚举,支持 Laravel 和 Lumen。
介绍
- 转自Jiannei/laravel-enum
- laravel-enum` 主要用来扩展项目中的常量使用,通过合理的定义常量可以使代码更加规范,更易阅读和维护。
概览
- 提供了多种实用的方式来实例化枚举
- 支持多语言本地化描述
- 支持表单验证,提供验证规则 enum,enum_key 和 enum_value,对请求参数中的参数进行枚举校验
- 支持路由中间件自动将 Request 参数转换成相应枚举实例
- 支持
Eloquent\Model
中的$casts
特性,将查询出的数据自动转换成枚举实例 - 提供了便捷的比较方法
is
、isNot
和in
,用于枚举实例之间的对比 - 内置了多种实用的枚举集:
- 标准的 Http 状态码枚举定义,方便在 API 返回响应数据时设置 Http 状态码;
CacheEnum
缓存枚举定义,一种统一项目中缓存 key 和缓存过期时间定义的方案;LogEnum
日志枚举定义,用于规范日志记录时的描述内容
安装
支持 Laravel 8/Lumen 8 以上版本:
配置项说明
Laravel
-
发布配置文件
- 添加路由中间件
Lumen
-
复制配置文件到
vendor/tomeet/laravel-enum/config/enum.php
,到config/enum.php
-
加载配置
-
添加路由中间件
- 注册服务容器
常规使用
-
定义
-
使用
-
本地化描述
-
枚举校验
-
枚举实例化:枚举实例化以后可以方便地通过对象实例访问枚举的 key、value 以及 description 属性的值。
-
枚举实例化进阶:(TransfrormEnums 中间件自动转换请求参数为枚举实例,使用的便是下面的 make 方法)
-
随机获取
-
toArray
- toSelectArray
枚举转换和校验
这一部分通过一个需求场景来描述:用户登录 API 需要校验传入的 identity_type 是否合法,并且根据不同的值调用不同的登录逻辑。
-
定义 IdentityTypeEnum
-
app/Http/Kernel.php
中添加路由中间件 -
config/enum.php
中配置 Request 参数和枚举之间的转换关系:参数 ⇒ 枚举 - Controller 中使用
说明:
- 扩展了验证规则 enum、enum_key、enum_value,可以对 Request 中的参数 identity_type 进行校验。
- 引入了
\Tomeet\Enum\Laravel\Http\Middleware\TransformEnums
到路由中间件中。 - 在 Controller 中以
$this->middleware('enum:false');
形式使用TransformEnums
中间件,并且向中间件传入了 false 参数。对应上面的UserTypeEnum::make('AdminiStrator', false);
,将不会对枚举参数进行大小写和类型校验 $request->get('identity_type')
获取到的是IdentityTypeEnum
实例,Enum 实例中提供了is
、isNot
和in
共 3 种枚举实例之间的比较方法
Model 中的枚举转换
为了实现上面的多账号类型登录,account 数据表中就需要有字段 identity_type
来描述账号类型。
Laravel 的 Eloquent\Model
提供了 $casts
特性,可以将查询出来的数据字段转换成指定类型。这里也可以利用这个特性,将 account 表中的 identity_type
转换成 IdentityTypeEnum
实例。
缓存枚举
为了提高应用性能,在应用中使用缓存是很常见的一种做法。但随着业务到达一定规模后,分散在各处的缓存使用,由于定义缓存 key 的方式不统一,缓存生效时间的不确定性,都会使后期缓存的维护变得困难。
-
定义:通过下面的方式将项目中所有缓存 key 和过期时间的定义统一到 CacheEnum 中,从而可以很直观的看出项目哪些地方使用了缓存,缓存多久失效。
- 使用
特别说明
laravel-enum 在下面2 个 package 功能的基础上,扩展增加了松散的(不区分大小写和数据类型) Request 参数转枚举实例,并内置提供了 HttpStatusCodeEnum 和 CacheEnum 等实用枚举集合。
- BenSampo/laravel-enum:支持枚举定义、提供各种实用的枚举校验和转换,但是缺少中间件转换,不支持松散的 Request 参数转枚举实例
- spatie/laravel-enum:支持中间件转换枚举
License
MIT