mikefrancis/laravel-unleash

Laravel的Unleash客户端

v0.10 2023-07-20 08:25 UTC

This package is auto-updated.

Last update: 2024-09-10 12:09:19 UTC


README

Codacy Badge Packagist Build Status codecov

Laravel的Unleash客户端。

安装

composer require mikefrancis/laravel-unleash

导出包配置

php artisan vendor:publish --provider="MikeFrancis\LaravelUnleash\ServiceProvider"

配置

配置文档可以在config/unleash.php中找到。

使用方法

use \MikeFrancis\LaravelUnleash\Unleash;

$unleash = app(Unleash::class);

if ($unleash->isFeatureEnabled('myAwesomeFeature')) {
  // Congratulations, you can see this awesome feature!
}

if ($unleash->isFeatureDisabled('myAwesomeFeature')) {
  // Check back later for more features!
}

$feature = $unleash->getFeature('myAwesomeFeature');

$allFeatures = $unleash->getFeatures();

门面

您可以使用Unleash门面

use Unleash;

if (Unleash::isFeatureEnabled('myAwesomeFeature')) {
  // Congratulations, you can see this awesome feature!
}

if (Unleash::isFeatureDisabled('myAwesomeFeature')) {
  // Check back later for more features!
}

$feature = Unleash::getFeature('myAwesomeFeature');

$allFeatures = Unleash::getFeatures();

或者使用命名通用的Feature门面

use Feature;

if (Feature::enabled('myAwesomeFeature')) {
  // Congratulations, you can see this awesome feature!
}

if (Feature::disabled('myAwesomeFeature')) {
  // Check back later for more features!
}

$feature = Feature::get('myAwesomeFeature');

$allFeatures = Feature::all();

动态参数

如果您的策略依赖于运行时的动态数据,您可以将额外的参数传递给功能检查函数

use \MikeFrancis\LaravelUnleash\Unleash;
use Config;

$unleash = app(Unleash::class);

$allowList = config('app.allow_list');

if ($unleash->isFeatureEnabled('myAwesomeFeature', $allowList)) {
  // Congratulations, you can see this awesome feature!
}

if ($unleash->isFeatureDisabled('myAwesomeFeature', $allowList)) {
  // Check back later for more features!
}

Blade

用于检查功能是否启用的Blade指令

@featureEnabled('myAwesomeFeature')
Congratulations, you can see this awesome feature!
@endfeatureEnabled

或者如果功能是禁用

@featureDisabled('myAwesomeFeature')
Check back later for more features!
@endfeatureDisabled

您目前不能在Blade模板指令中使用动态策略参数。

中间件

此包包含一个中间件,它将根据功能是否启用或禁用来拒绝路由。

要使用中间件,请将以下内容添加到您的app/Http/Kernel.php

protected $routeMiddleware = [
    // other middleware
    'feature.enabled' => \MikeFrancis\LaravelUnleash\Middleware\FeatureEnabled::class,
    'feature.disabled' => \MikeFrancis\LaravelUnleash\Middleware\FeatureDisabled::class,
];

然后您可以在您的路由

Route::get('/new-feature-path', function () {
    //
})->middleware('feature.enabled:myAwesomeFeature');

Route::get('/terrible-legacy-path', function () {
    //
})->middleware('feature.disabled:myAwesomeFeature');

或控制器中使用此中间件

class ExampleController extends Controller
{
    public function __construct()
    {
        $this->middleware('feature.enabled:myAwesomeFeature');
        // or
        $this->middleware('feature.disabled:myAwesomeFeature');
    }
}

您目前不能在中间件中使用动态策略参数。