ahmadrosid / laravel-env-feature-flags
这是一个简单的包,用于在 Laravel 应用中使用环境变量管理功能标志。
1.1.2
2024-04-24 17:52 UTC
Requires
- illuminate/support: ^10.0
README
有时,您需要开发一个想要在生产环境中测试的功能,但又不想让用户看到它。这就是使用环境变量文件(.env)存储功能标志变得非常有用。这个包将允许您轻松地完成这项工作。
安装
composer require ahmadrosid/laravel-env-feature-flags
如何使用
1. 在 .env
中定义功能标志:
在您的 .env
文件中,您可以将功能标志定义为环境变量。例如
FEATURE_NEW_DESIGN=true FEATURE_PAYMENT_GATEWAY=false
2. 在您的代码中访问功能标志:
您可以使用包提供的 Features 门面来检查功能标志的状态
use Ahmadrosid\FeatureFlags\Features; if (Features::enabled('new_design')) { // Code for the new design feature } else { // Code for the old design }
请注意,enabled 方法期望一个 snake_case 字符串作为功能名称,所以 FEATURE_NEW_DESIGN
变为 new_design
。
3. 在 Blade 模板中添加功能标志检查:
如果您想根据功能标志有条件地渲染 Blade 模板的部分内容,可以使用包提供的 @feature Blade 指令
@feature('new_design') <!-- Code for the new design feature --> @else <!-- Code for the old design --> @endfeature
您还可以使用名为 @hasAccess
的 Blade 指令。这个指令可以在您的 Blade 模板中使用,以检查当前用户是否有权访问特定的功能。如果用户没有权限,您可以显示一条消息或执行任何其他需要的操作。
@hasAccess('new_design') <!-- Code for the new design feature --> @else <div class="alert alert-warning"> You don't have access to the new design feature. </div> @endhasAccess
要启用此功能,您需要在 .env
文件中添加此标志。
FEATURE_NEW_DESIGN_USERS=1,2,3
许可证
MIT