leettech/laravel-flagger

此包的最新版本(v2.1.0)没有可用的许可信息。

v2.1.0 2018-04-20 14:05 UTC

This package is auto-updated.

Last update: 2024-09-28 09:09:22 UTC


README

Flagger 是一个旨在帮助您在 Laravel 项目中启用功能标志的包。

版本兼容性

安装

要通过 composer 安装,只需在您的 composer.json 文件中添加以下内容

{
    "require": {
        "leettech/laravel-flagger": "~2.0"
    }
}

然后运行 composer install

快速安装

您也可以使用以下命令简化上述安装

composer require "leettech/laravel-flagger=~2.0"

配置

安装 Flagger 包后,在您的 config/app.php 配置文件中注册 FlaggerServiceProvider

'providers' => [
    // Other service providers...
    Leet\Providers\FlaggerServiceProvider::class,
],

此外,将 Flagger facade 添加到 app 配置文件中的别名数组中

'aliases' => [
    // Other aliases...
    'Flagger' => Leet\Facades\Flagger::class,
],

然后运行迁移脚本来创建 featuresflaggables

php artisan migrate

发布包配置

php artisan vendor:publish --provider="Leet\Providers\FlaggerServiceProvider"

然后,在您的 config/flagger.php 配置文件中,指定哪个模型将具有与之关联的功能标志(默认设置为 App\User::class)。

使用

首先,请确保您已将功能插入到数据库中的 features 表中。您可以使用模型 Leet\Models\Feature 来完成此操作

\Leet\Models\Feature::create([
    'name' => 'notifications',
    'description' => 'Notifications feature'
]);

flag

使用 \Flagger::flag($flaggable, $feature) 将功能附加到模型上

$user = \App\User::first();
\Flagger::flag($user, 'notifications');

您还可以将 Leet\Models\FlaggerTrait 添加到模型中,以便从其中访问 flagger 方法

class User extends Model
{
    use \Leet\Models\FlaggerTrait;
}
$user = \App\User::first();
$user->flag('notifications');

flagMany

使用 \Flagger::flagMany($flaggables, $feature) 将功能附加到模型集合上

$users = \App\User::all();
\Flagger::flagMany($users, 'notifications');

hasFeatureEnabled

在任何应用程序位置,您都可以检查用户是否有权访问功能

if ($user->hasFeatureEnabled('notifications')) {
    doSomething();
}

FlaggerMiddleware

要使用 FlaggerMiddleware,您必须在应用程序内核中声明它

protected $routeMiddleware = [
    // Other middleware...
    'flagger' => \Leet\Middleware\FlaggerMiddleware::class,
];

在任何认证路由上

Route::get('notifications', 'NotificationsController@index')->middleware('flagger:notifications');

Route::group(['middleware' => 'flagger:notifications'], function () {
    Route::get('notifications', 'NotificationsController@index');
    Route::post('notifications', 'NotificationsController@store')
});

获取模型的启用功能

通过将 Leet\Models\FlaggerTrait 添加到您的模型中,您能够访问其启用的功能

// returns the features a user have access to
$user->features;

Flagger 命令

flagger 命令接受一个整数、一个数组或一个包含整数列表的 csv 路径,并将标志添加到每个元素中

php artisan flagger notifications 1
// OR
php artisan flagger notifications 1 2 3
// OR
php artisan flagger notifications users.csv
// OR
php artisan flagger notifications users.csv --chunk=100

在尝试将标志添加到任何实体之前,请确保您已创建标志。