evolabs/laravel-feature-flags 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

evolabs/laravel-feature-flags

最新稳定版本:1.0.4

Composer 安装命令:

composer require evolabs/laravel-feature-flags

包简介

Add simple database feature flags

README 文档

README

This package allows you to add flags in your application and thus easily turn on/off its features.

Installation

You can install the package via composer:

composer require evolabs/laravel-feature-flags

The package will automatically register its service provider.

You can publish and run the migrations with:

php artisan vendor:publish --provider="Evolabs\FeatureFlags\FeatureFlagsServiceProvider" --tag=migrations
php artisan migrate

You can publish and customize config file with:

php artisan vendor:publish --provider="Evolabs\FeatureFlags\FeatureFlagsServiceProvider" --tag=config

Usage

Features are storing in database. To use them in your application you should first create them e.g. via database seeder class:

use Evolabs\FeatureFlags\Models\Feature;
use Illuminate\Database\Seeder;

class FeaturesTableSeeder extends Seeder
{
    public function run()
    {
        Feature::query()->create(['name' => 'information_pages'];
        Feature::query()->create(['name' => 'locale_change', 'group' => 'admin'];
    }
}

All public methods are available via the facade class Evolabs\FeatureFlags\Facades\Facades.

Check feature is accessible

Features::isAccessible('information_pages')

Add middleware to ensure that feature is enabled

Route::get('/', 'YourController@index')->middleware('feature:information_pages')

Toggle features with artisan commands

php artisan feature:on information_pages

php artisan feature:off information_pages

List all features

Features::all()

It will returns a collection of FeatureData objects:

namespace Evolabs\FeatureFlags\DataTransferObjects;

class FeatureData
{
    public function __construct(
        public readonly string $name,
        public readonly ?string $group,
        public readonly bool $is_enabled
    ) {
    }
}

Create feature groups e.g. for back office or frontend area

Feature::query()->create(['name' => 'information_pages', 'group' => 'admin'];
Feature::query()->create(['name' => 'media_library', 'group' => 'admin'];

You can then load all the features in group:

Features::all('admin')

Use feature flags in blade views

@feature('information_pages')
    <p>Feature flag `information_pages` is turned on.</p>
@endfeature

Use feature flags with Inertia and vue

You can load feature flags in HandleInertiaRequests middleware class:

use Evolabs\FeatureFlags\Facades\Features;
use Illuminate\Http\Request;
use Inertia\Middleware;

class HandleInertiaRequests extends Middleware
{
    /**
     * The root template that's loaded on the first page visit.
     *
     * @see https://inertiajs.com/server-side-setup#root-template
     *
     * @var string
     */
    protected $rootView = 'admin/app';

    /**
     * Defines the props that are shared by default.
     *
     * @see https://inertiajs.com/shared-data
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function share(Request $request)
    {
        return [
            ...parent::share($request),
            'features' => Features::all('admin')->pluck('is_enabled', 'name')
        ];
    }
}

and use them in your vue template:

<div v-if="$page.props.features.information_pages">
    <p>Feature flag `information_pages` is turned on.</p>
</div>

统计信息

  • 总下载量: 34
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-10-20

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固