PHP code example of dive-be / laravel-feature-flags

1. Go to this page and download the library: Download dive-be/laravel-feature-flags library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

dive-be / laravel-feature-flags example snippets


return [

    /**
     * The name of the cache key that will be used to cache your app's features.
     */
    'cache_key' => 'feature_flags',

    /**
     * The feature model that will be used to retrieve your app's features.
     */
    'feature_model' => Dive\FeatureFlags\Models\Feature::class,
];

Dive\FeatureFlags\Models\TranslatableFeature::class

// ...
$table->json('message')->nullable();
$table->timestamp('disabled_at')->nullable();
// ...

use Dive\FeatureFlags\Models\Feature;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;

class FeaturesTableSeeder extends Seeder
{
    public function run()
    {
        DB::table('features')->upsert([
            [
                'description' => 'Registrations that come through our partnerships',
                'label' => 'Public registrations',
                'message' => 'The registration period has ended. Thanks for participating in our programme.',
                'name' => 'registrations',
                'scope' => Feature::getDefaultScope(),
            ],
            [
                'description' => 'Display the App version on the homepage',
                'label' => 'Application version',
                'message' => 'Version is hidden',
                'name' => 'version',
                'scope' => Feature::getDefaultScope(),
            ],
        ], ['scope', 'name'], ['description', 'label', 'message']);
    }
}

use Dive\FeatureFlags\Facades\Feature;

Feature::find('dashboard');

use Feature;

Feature::disabled('dashboard');

feature('dashboard');
feature_disabled('dashboard');
feature_enabled('dashboard');
feature_verify('dashboard');

use Dive\FeatureFlags\Contracts\Feature;

public function index(Feature $feature)
{
    $feature->verify('dashboard');

    return view('layouts.dashboard');
}

app('feature')->find('dashboard');

use Dive\FeatureFlags\Models\Feature;

class AppServiceProvider extends ServiceProvider
{
    public function boot()
    {
        Feature::setDefaultScope('my-wonderful-app');
    }
}

@can('feature', 'dashboard')
    <a href="/dashboard" target="_blank">View Dashboard</a>
@else
    <small>Dashboard is currently disabled</small>
@endcan

Route::get('registrations', [RegistrationsController::class, 'index'])->middleware('feature:registrations');

class RegistrationsController extends Controller
{
    public function __construct(Feature $feature)
    {
        $feature->verify('registrations');
    }
    
    public function index()
    {
        return view('registrations.index');
    }
}

use Dive\FeatureFlags\Facades\Feature;
use Dive\Wishlist\Facades\Wishlist;

class HeartButton extends Component
{
    public function add($id)
    {
        Feature::verify('wishlist');
        
        Wishlist::add($id);
    }

    public function render()
    {
        return view('livewire.heart-button');
    }
}

Route::get(...)->middleware('can:feature,dashboard');

Gate::authorize('feature', 'dashboard');
shell
php artisan feature:install
shell
php artisan make:seeder FeaturesTableSeeder

php artisan feature:toggle {name} {scope?}
shell
php artisan feature:list
shell
php artisan feature:clear