synergitech/laravel-magic-enums
Composer 安装命令:
composer require synergitech/laravel-magic-enums
包简介
A Laravel package that adds extra power to your PHP enums, and lets you use them in your frontend with type definitions.
README 文档
README
Laravel Magic Enums
Have you ever wanted to reference your PHP enums in your frontend code but ended up (or didn't want to end up) duplicating them manually? Well here is your answer.
Installing
You need both sides to get started.
$ composer require synergitech/laravel-magic-enums $ npm install --save laravel-magic-enums
Getting Started
- Add the trait and interface from this package to your enums. We recommend you don't include sensitive information in your enums.
<?php namespace App\Enums; use SynergiTech\MagicEnums\Interfaces\MagicEnum; use SynergiTech\MagicEnums\Traits\HasMagic; enum YourEnum: string implements MagicEnum { use HasMagic; ...
-
Generate an export of enums with
php artisan laravel-magic-enums:generate. This will create a file atresources/js/magic-enums/index.js. -
Use the exported enums in your frontend like so. Your IDE will infer any types from your enums:
import { enums } from 'resources/js/magic-enums/index.js'; const { TestingEnums } = enums;
- During development, you can have Vite react automatically to changes in your enums. The
laravel-magic-enums/viteplugin provides a simple way to do this. Under the hood, this calls thephp artisan laravel-magic-enums:generatecommand, and can customise it. For example, here's how to use it with some customisations:
import { defineConfig } from 'vite'; import laravel from 'laravel-vite-plugin'; import vue from '@vitejs/plugin-vue'; import { laravelMagicEnums } from "laravel-magic-enums/vite"; export default defineConfig({ plugins: [ laravel({ ... }), vue({ ... }), laravelMagicEnums({ input: 'app/Enums', output: 'resources/js/my-enums', format: true, }), ], ...
- We recommend adding the exported enums to the
.gitignorefile.
Advanced Usage
Sub Enums
You may choose to have an array within your enum of a subset of the values for a specific purpose or grouping.
If you use the PHP attribute SynergiTech\MagicEnums\Attributes\AppendConstToMagic, then an extra enum representing this will be available in the frontend.
You may also have an array which maps some or all of the values of the enum to a different string.
If you use the PHP attribute SynergiTech\MagicEnums\Attributes\AppendValueToMagic, then an extra enum representing this will be available in the frontend.
For example:
<?php namespace App\Enums; use SynergiTech\MagicEnums\Attributes\AppendConstToMagic; use SynergiTech\MagicEnums\Attributes\AppendValueToMagic; use SynergiTech\MagicEnums\Interfaces\MagicEnum; use SynergiTech\MagicEnums\Traits\HasMagic; enum TestingEnum: string implements MagicEnum { use HasMagic; case First = 'first'; case Second = 'second'; case Third = 'third'; #[AppendConstToMagic] public const JUST_ONE = [ self::First, ]; #[AppendValueToMagic] public const COLOUR = [ self::First->value => 'red', ]; }
Will create the output:
TestingEnum: { First: { "name": "First", "value": "first", "colour": "red" }, Second: { "name": "Second", "value": "second", "colour": null }, Third: { "name": "Third", "value": "third", "colour": null } }, TestingEnumJustOne: { First: { "name": "First", "value": "first", "colour": "red" } }
Extending
If you wish to have more control over appending values to your magic enums for the frontend, you can extend the current trait using something along the lines of app/Traits/CustomMagic.php as long as you always follow the interface and provide the function.
synergitech/laravel-magic-enums 适用场景与选型建议
synergitech/laravel-magic-enums 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 7.6k 次下载、GitHub Stars 达 9, 最近一次更新时间为 2025 年 05 月 29 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「laravel」 「enums」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 synergitech/laravel-magic-enums 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 synergitech/laravel-magic-enums 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 synergitech/laravel-magic-enums 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Enums for Laravel Enso
Shared backed enums classifying HTML elements by category (block, inline, list, table, void, metadata, root) for the UI Awesome ecosystem.
Helpers for handling enums and enum translations in Laravel
A shared set of open-source enums for frequently used domains, helping reduce duplication and improve consistency across software projects.
Production-ready PHP SDK for Iberbanco API v2 - Enterprise banking platform with 80+ currencies, professional enums, type-safe DTOs, optimized performance, and comprehensive financial services
Auto-generate TypeScript type definitions from Laravel Models, Enums, Casts, and Requests.
统计信息
- 总下载量: 7.6k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 9
- 点击次数: 3
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-05-29