intrfce/laravel-frontend-enums
Composer 安装命令:
composer require intrfce/laravel-frontend-enums
包简介
Stop using 'magic' strings/numbers in your frontend Javascript and use your actual application enums instead.
README 文档
README
Publish your PHP enums to the front-end of your application, so you can refer to them in your JavaScript code.
This means less reliance on "magic strings".
// Before: if (myValue === 'name') { // Do something } // After import {UserProfileField} from './UserProfileField.enum.js'; if (myValue === UserProfileField.Name) { // Do something. }
Installation
You can install the package via composer:
composer require intrfce/laravel-frontend-enums
Usage
Just add the attribute!
Add the #[PublishEnum] attribute to any enum you want published to the frontend - it's that easy.
use Intrfce\LaravelFrontendEnums\Attributes\PublishEnum; #[PublishEnum] enum MyEnum: string { case Foo = 'foo'; case Bar = 'bar'; }
Then run the publish command:
php artisan publish:enums-to-javascript
Your enums will be waiting at resources/js/Enums with the extension .enum.js:
import {MyEnum} from './Enums/MyEnum.enum.js'; console.log(MyEnum.Foo); // 'foo'
Configuration
Publish the config file to customise the output path and discovery directories:
php artisan vendor:publish --tag=config --provider="Intrfce\LaravelFrontendEnums\LaravelFrontendEnumsServiceProvider"
// config/laravel-frontend-enums.php return [ // Customise the output directory. 'publish_to' => resource_path('js/Enums'), // Customise the folders scanned for enum classes 'discover_in' => [ app_path(), ], // Always output as typescript enums. 'as_typescript' => true, ];
The discover_in array supports glob patterns, which is useful for modular or monorepo layouts:
'discover_in' => [ app_path(), base_path('app-modules/*/src'), ],
Typescript Support
Enable TypeScript output globally via the config file:
'as_typescript' => true,
Or override per-enum using the attribute:
#[PublishEnum(asTypescript: true)] // Force TypeScript for this enum #[PublishEnum(asTypescript: false)] // Force JavaScript for this enum #[PublishEnum] // Follow the global config setting
TypeScript enums are output as .ts files:
export enum MyEnum { Foo = "foo", Bar = "bar", }
Automatically generate javascript files on change.
You can use the vite-plugin-watch package from lepikhinb to automatically generate your javascript files when you make changes to your PHP enums:
npm install -D vite-plugin-watch
Then add the plugin to your vite.config.js:
import { defineConfig } from "vite" import { watch } from "vite-plugin-watch" export default defineConfig({ plugins: [ watch({ pattern: "app/Enums/**/*.php", command: "php artisan publish:enums-to-javascript", }), ], })
Changelog
Please see CHANGELOG for more information what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email dan@danmatthews.me instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.
intrfce/laravel-frontend-enums 适用场景与选型建议
intrfce/laravel-frontend-enums 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 27.56k 次下载、GitHub Stars 达 23, 最近一次更新时间为 2023 年 10 月 24 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「intrfce」 「laravel-frontend-enums」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 intrfce/laravel-frontend-enums 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 intrfce/laravel-frontend-enums 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 intrfce/laravel-frontend-enums 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
统计信息
- 总下载量: 27.56k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 23
- 点击次数: 12
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-10-24