PHPackages                             tomeet/laravel-enum - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. tomeet/laravel-enum

ActiveLibrary

tomeet/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

v1.0.3(3y ago)091MITPHP

Since Jun 21Pushed 3y ago1 watchersCompare

[ Source](https://github.com/tomeet/laravel-enum)[ Packagist](https://packagist.org/packages/tomeet/laravel-enum)[ RSS](/packages/tomeet-laravel-enum/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (4)Dependencies (3)Versions (5)Used By (1)

 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。

[![Test](https://github.com/Tomeet/laravel-enum/workflows/Test/badge.svg?branch=main)](https://github.com/Tomeet/laravel-enum/workflows/Test/badge.svg?branch=main)[![StyleCI](https://camo.githubusercontent.com/fd64bf15ebf4b7b9b7321031edf021a671c2546347f6fea54c24fe06dc950455/68747470733a2f2f6769746875622e7374796c6563692e696f2f7265706f732f3331363930373939362f736869656c643f6272616e63683d6d61696e)](https://github.styleci.io/repos/316907996?branch=main)[![Latest Stable Version](https://camo.githubusercontent.com/0a993d2bcce34281a3e31338ea4a4f9f53ead7f9416c28839d2ec17ba04c014e/687474703a2f2f706f7365722e707567782e6f72672f546f6d6565742f6c61726176656c2d656e756d2f76)](https://packagist.org/packages/Tomeet/laravel-enum)[![Total Downloads](https://camo.githubusercontent.com/c21f709245325e44c09b26156f47a96af42e94311d6f0ca1fad3d3b6402907e7/687474703a2f2f706f7365722e707567782e6f72672f546f6d6565742f6c61726176656c2d656e756d2f646f776e6c6f616473)](https://packagist.org/packages/Tomeet/laravel-enum)[![Monthly Downloads](https://camo.githubusercontent.com/4db39f8c3ca94947fc92bb56c66a9cbbfada223ca154398b8f5192eaeb7e9290/687474703a2f2f706f7365722e707567782e6f72672f546f6d6565742f6c61726176656c2d656e756d2f642f6d6f6e74686c79)](https://packagist.org/packages/Tomeet/laravel-enum)[![Latest Unstable Version](https://camo.githubusercontent.com/f8d6969e63d465cfd69c81ca0b1398b0e49b8891c56c3ef38af65ea39ff62f5a/687474703a2f2f706f7365722e707567782e6f72672f546f6d6565742f6c61726176656c2d656e756d2f762f756e737461626c65)](https://packagist.org/packages/Tomeet/laravel-enum)[![License](https://camo.githubusercontent.com/28da4156a670a08adc4915d7dbd7188ee743bb19bd1003139240306d29f1e3b5/687474703a2f2f706f7365722e707567782e6f72672f546f6d6565742f6c61726176656c2d656e756d2f6c6963656e7365)](https://packagist.org/packages/Tomeet/laravel-enum)

介绍
--

[](#介绍)

- [转自Jiannei/laravel-enum](https://github.com/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 以上版本：

```
$ composer require Tomeet/laravel-enum -vvv
```

### 配置项说明

[](#配置项说明)

```
// config/enum.php

return [
    'localization' => [
        'key' => env('ENUM_LOCALIZATION_KEY', 'enums'),// 语言包的文件名。如果 APP_LOCALE=zh_CN，则这里对应 resources/lang/zh_CN/enums.php
    ],

    // 通过 transform 中间件将 request 中的参数转换成枚举实例时定义对应关系
    // 你可以将请求参数中用到的枚举定义在下面，通过中间件，将会被自动转换成枚举类
    'transformations' => [
        // 参数名 => 对应的枚举类

    ],
];
```

### Laravel

[](#laravel)

- 发布配置文件

```
$ php artisan vendor:publish --provider="Tomeet\Enum\Laravel\Providers\LaravelServiceProvider"
```

- 添加路由中间件

```
// app/Http/Kernel.php
