matthiaswilbrink/laravel-feature-toggles

此包的最新版本(0.5.0)没有可用的许可证信息。

开启或关闭应用程序的部分功能

0.5.0 2022-06-09 13:18 UTC

This package is not auto-updated.

Last update: 2024-09-26 23:58:09 UTC


README

无需停机即可开启或关闭应用程序的部分功能。

晚上部署功能,当老板按下那个大红色按钮时切换它。
(大红色按钮不包括在内)

功能

  • 在配置中设置功能的初始状态
  • 使用laravel的缓存来限制数据库负载
  • 方便的命令来开启/关闭功能

安装

此包可在composer上获取。
要安装laravel-feature-toggles,可以运行以下命令

composer require matthiaswilbrink/laravel-feature-toggles

安装后,可以使用以下命令发布配置

php artisan vendor:publish --provider="MatthiasWilbrink\FeatureToggle\Providers\FeatureToggleServiceProvider"

由于此包使用数据库来跟踪功能的初始状态,您在安装此包后需要进行迁移

php artisan migrate

使用方法

读取

Blade

已创建一个自定义blade指令。
在blade文件中执行以下操作

[...]
@feature('name_of_my_feature_as_string')
    This will show when the feature is enabled
@endfeature
[...]

也可以使用else子句。

[...]
@feature('name_of_my_feature_as_string')
    This will show when the feature is enabled.
@else
    This will show when the feature is disabled.
@endfeature
[...]

辅助函数

辅助函数feature(string $name)返回给定功能的当前状态。

if (feature('name_of_my_feature_as_string')){
    //do something
}

外观

已注册别名。

// Check if a feature is enabled
if (Feature::isEnabled('name_of_my_feature_as_string')){
    //do something
}

// A shorter alias
if (Feature::isOn('name_of_my_feature_as_string')){
    //do something
}

依赖注入

当然,您也可以将FeatureManager注入以实现相同的目标。

public function someMethod(FeatureManager $featureManager)
{
    if ($featureManager->isEnabled('name_of_my_feature_as_string')){
        //do something
    }
}

其他活动

您可能还希望允许用户启用或禁用功能。(例如,圣诞节主题)

public function turnTheSnowFlakesOn(FeatureManager $featureManager)
{
    $featureManager->enable('homepage_snowflakes_animation');
}

关于缓存的一个注意事项

如果开启了缓存,每次启用/禁用功能时都会清除并重建缓存。
如果需要,您可以使用php artisan feature:clear-cache清除缓存。
这将仅清除功能缓存,而不会清除常规应用程序缓存。