russellhudson/laravel-feature-flags-expanded

一个用于管理Laravel项目中功能标记的简单包。

dev-master 2023-05-08 14:38 UTC

This package is auto-updated.

Last update: 2024-09-08 17:41:20 UTC


README

快速通道

if(Feature::isEnabled('feature_name', $obj)) {
     <!---content---->
}
NE
@feature('my_awesome_feature',$obj)
    <p>This paragraph will be visible only if "my_awesome_feature" is enabled!</p>
@endfeature



在ScholarPath App中的以下对象中可以启用功能标记:($user, $role, $silo, $district, $school)

基本用法

您可以使用两种方式使用功能:全局操作或针对特定实体操作。

全局启用/禁用功能

声明新功能

假设您有一个新的功能,您希望将其隐藏直到某个时刻。我们将其称为"page_code_cleaner"。让我们将其添加到我们的应用程序中

Feature::add('page_code_cleaner', false);

很简单,对吧?如您所想,第一个参数是功能名称。第二个参数是我们指定的布尔值,用于定义功能的当前状态。

  • true 表示 该功能对所有人启用
  • false 表示 该功能被隐藏,没有人可以使用它/看到它

就这样。

检查功能是否启用

现在,让我们为我们的示例提供一个更好的上下文。我们正在构建一个CMS,我们的"page_code_cleaner"用于... 清理我们的HTML代码。让我们假设我们有一个这样的控制器。

class CMSController extends Controller {
    public function getPage($pageSlug) {
        
        // here we are getting our page code from some service
        $content = PageService::getContentBySlug($pageSlug);
        
        // here we are showing our page code
        return view('layout.pages', compact('content'));
    }
}

现在,我们想要部署新服务,但我们不希望用户可以使用,因为市场团队要求我们下周发布。LaravelFeature帮助我们做到了这一点

class CMSController extends Controller {
    public function getPage($pageSlug) {
        
        // here we are getting our page code from some service
        $content = PageService::getContentBySlug($pageSlug);
        
        // feature flagging here!
        if(Feature::isEnabled('page_code_cleaner')) {
            $content = PageCleanerService::clean($content);
        }
        
        // here we are showing our page code
        return view('layout.pages', compact('content'));
    }
}

哇!现在,只有当"page_code_cleaner"功能启用时,才会执行特定的服务代码。

更改功能激活状态

显然,使用Feature类,我们可以轻松地切换功能激活状态。

// release the feature!
Feature::enable('page_code_cleaner');

// hide the feature!
Feature::disable('page_code_cleaner');

删除功能

即使它不是很常用,您也可以使用以下方式轻松地删除功能

Feature::remove('page_code_cleaner');

警告: 请确保您所做的是正确的。如果您从系统中删除了功能,如果在代码库中仍然存在对已删除功能的检查,您将遇到异常。

与视图一起工作

我真的喜欢blade指令,它们帮助我编写更优雅的代码。我准备了一个自定义blade指令,@feature

<div>This is an example template div. Always visible.</div>

@feature('my_awesome_feature')
    <p>This paragraph will be visible only if "my_awesome_feature" is enabled!</p>
@endfeature

<div>This is another example template div. Always visible too.</div>

一个非常方便的快捷方式!

为特定用户/实体启用/禁用功能

尽管我们之前看到的东西很有用,但LaravelFeature 不仅仅是关于在功能上推开关。有时,业务需求需要更大的灵活性。想想一个金丝雀发布:我们只想向特定用户推出功能。或者,也许只为一个测试用户。

为特定用户启用功能管理

LaravelFeature使这成为可能,并且与向我们的User类添加特性一样简单。

事实上,您需要做的只是

  • LaravelFeature\Featurable\Featurable特性添加到User类中
  • 让同一个类 实现FeaturableInterface接口
...

class User extends Authenticatable implements FeaturableInterface
{
    use Notifiable, Featurable;
    
...

这就完成了!LaravelFeature现在已经知道该怎么做。

状态优先级

请注意,如果您已经全局启用了功能,以下所有内容都是无效的。要为特定用户激活功能,您首先需要禁用它。

Laravel-Feature 首先检查该功能是否全局启用,然后向下到实体级别

为特定用户启用/禁用功能

$user = Auth::user();

// now, the feature "my.feature" is enabled ONLY for $user!
Feature::enableFor('my.feature', $user);

// now, the feature "my.feature" is disabled for $user!
Feature::disableFor('my.feature', $user);

检查特定用户是否启用了该功能

$user = Auth::user();

if(Feature::isEnabledFor('my.feature', $user)) {
    
    // do amazing things!
    
}

其他注意事项

新增针对Blade指令的ELSEFEATURE功能 LaravelFeature还提供了一个Blade指令来检查特定用户是否启用了该功能。您可以使用@featurefor blade标签

@featurefor('my_awesome_feature',$obj)
    <p>This paragraph will be visible only if "my_awesome_feature" is enabled!</p>
@elsefeaturefor
    <p>Something else</p>
@endfeaturefor

高级功能

好吧,现在我们已经了解了基础知识,让我们提高标准!

为其他实体启用功能管理

正如我之前所说,您只需使用Featurable特性并在User模型中实现FeaturableInterface,就可以轻松地为用户添加功能管理。然而,在构建关系时,我决定实现一个多对多多态关系。这意味着您可以为任何模型添加功能管理

让我们举一个例子:假设您有一个Role模型,您用它来实现用户的基本角色系统。这是因为您有管理员和普通用户。

所以,您推出了一个惊人的杀手级功能,但只想为管理员启用它。如何做到这一点?很简单。回顾一下

  • Featurable特性添加到Role模型中;
  • 确保Role模型实现了FeaturableInterface

让我们将角色-用户关系视为一对一关系。

您在User类上可能有一个role()方法,对吧?很好。您已经知道接下来该怎么做

// $role is the admin role!
$role = Auth::user()->role;

...

Feature::enableFor('my.feature', $role);

...

if(Feature::isEnabledFor('my.feature', $role)) {

    // this code will be executed only if the user is an admin!
    
}

扫描目录以查找功能

当我制作这个包时,启发我的那个包的一个很好的额外功能是能够“扫描”视图,找到@feature声明,然后如果系统尚未存在,将这些扫描到的功能添加到系统中。

我创建了一个简单的artisan命令来完成这个任务。

$ php artisan feature:scan

该命令将使用一个专用服务来获取resources/views文件夹,并扫描每个Blade视图以查找@feature指令。然后,它将输出搜索结果。

试试看,您会喜欢的!

鸣谢

许可协议

MIT许可协议(MIT)。有关更多信息,请参阅许可文件