bonch.dev / laravel-unleash
Laravel 的 Unleash 客户端
v0.11.0
2024-08-13 00:24 UTC
Requires
- guzzlehttp/guzzle: >6.3
- illuminate/contracts: >6
- illuminate/http: >6
- illuminate/support: >6
Requires (Dev)
- phpunit/phpunit: ^9.0
- squizlabs/php_codesniffer: ^3.5
README
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');
}
}
您目前不能在中间件中使用动态策略参数。