PHP code example of ericdowell / feature-toggle

1. Go to this page and download the library: Download ericdowell/feature-toggle 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/ */

    

ericdowell / feature-toggle example snippets


if (feature_toggle_api()->isActive('Example')) {
    // do something
}

if (feature_toggle('Example')) {
    // do something
}

if (feature_toggle('Example', false)) {
    // do something when toggle is inactive
}
// OR
if (feature_toggle('Example', 'off')) {
    // do something when toggle is inactive
}

use Illuminate\Support\Facades\Route;

// Passing all three parameters, changing abort to 403 status code.
Route::get('user/billing')->middleware('featureToggle:subscription,true,403')->uses('User\\BillingController@index')->name('billing.index');
// Passing two parameters.
Route::get('user/subscribe')->middleware('featureToggle:subscription,true')->uses('User\\SubscribeController@index')->name('subscribe.index');
// Passing just the name.
Route::get('user/trial')->middleware('featureToggle:trial')->uses('User\\TrialController@index')->name('trial.index');

use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

class Kernel extends ConsoleKernel
{
    protected function schedule(Schedule $schedule)
    {
        $schedule->command('inspire')
                 ->hourly()
                 ->when(feature_toggle('Inspire Command'));
    }
}

use Illuminate\Support\Facades\Validator;

Validator::make(request()->all(), [
    'phone' => ',
]);

use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;

Validator::make(request()->all(), [
    'phone' => Rule::

use FeatureToggle\Facades\FeatureToggleApi;
use Illuminate\Support\Facades\Validator;

Validator::make(request()->all(), [
    'phone' => FeatureToggleApi::

$redisProvider = feature_toggle_api('redis');
// return false
$redisProvider->isActive('Example Off');

$sessionProvider = feature_toggle_api('session');
// return false
$sessionProvider->isActive('Example False');

$localProvider = feature_toggle_api()->getLocalProvider();
// return false
$localProvider->isActive('Example');

// Returns by reference.
$conditionalProvider = feature_toggle_api()->getConditionalProvider();
$conditionalProvider->setToggle('Example', function() { return true; });
// return true
$conditionalProvider->isActive('Example');

// Request ?feature=Example
$queryStringProvider = feature_toggle_api()->getQueryStringProvider();
// return true
$queryStringProvider->isActive('Example');

$eloquentProvider = feature_toggle_api()->getEloquentProvider();
// return false
$eloquentProvider->isActive('Example');

feature_toggle_api()->setProviders([
    [
        'driver' => 'conditional',
    ],
    [
        'driver' => 'eloquent',
    ],
]);

return [
    'drivers' => [
        'local' => \App\FeatureToggle\LocalToggleProvider::class,
        'redis' => \App\FeatureToggle\RedisToggleProvider::class,
        'session' => \App\FeatureToggle\SessionToggleProvider::class,
    ],
];

return [
    'providers' => [
        [
            'driver' => 'session',
        ],
        [
            'driver' => 'conditional',
        ],
        [
            'driver' => 'redis',
        ],
        [
            'driver' => 'local',
        ],
    ],
];

return [
    // ...
    'toggles' => [
        'Example' => env('FEATURE_EXAMPLE'),
        'Show Something' => env('FEATURE_SHOW_SOMETHING'),
    ],
];

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;

// calling conditional function is deferred by default
feature_toggle_api()->setConditional('Example', function (Request $request) {
    $user = $request->user();
    return $user instanceof \App\User && $user->email === '[email protected]';
});

// OR call right away by passing false as $defer parameter
feature_toggle_api()->setConditional('Example', function () {
    return Cache::get('feature:example');
}, false);

[
    'providers' => [
        [
            'driver' => 'eloquent',
        ],
    ],
];

feature_toggle_api()->setProviders([
    [
        'driver' => 'conditional',
    ],
    [
        'driver' => 'eloquent',
    ],
    [
        'driver' => 'local',
    ],
]);

return [
    'options' => [
        'useMigrations' => true,
    ],
];

use FeatureToggle\Api;

Api::useMigrations();

return [
    'providers' => [
        [
            'driver' => 'eloquent',
            'model' => \App\FeatureToggle::class
        ],
    ],
];

[
    'providers' => [
        [
            'driver' => 'querystring',
        ],
    ],
];

return [
    'providers' => [
        [
            'driver' => 'querystring',
            'activeKey' => 'active',
            'inactiveKey' => 'inactive',
        ],
    ],
];

return [
    'providers' => [
        [
            'driver' => 'querystring',
            'apiKey' => env('FEATURE_TOGGLE_API_KEY'),
            // Optionally change to something different.
            // 'apiInputKey' => 'feature_toggle_api_token',
        ],
    ],
];

[
    'providers' => [
        [
            'driver' => 'redis',
        ],
    ],
];

return [
    'providers' => [
        [
            'driver' => 'redis',
            'key' => 'toggles', // Optional, otherwise 'feature_toggles'
            'prefix' => 'feature', // Optional
            'connection' => 'toggles', // Must match key in database.redis.{connection}
        ],
    ],
];

[
    'providers' => [
        [
            'driver' => 'session',
        ],
    ],
];
bash
php artisan vendor:publish --tag="feature-toggle-config"
bash
php artisan vendor:publish --tag="feature-toggle-migrations"
bash
php artisan migrate
blade
<script>
    window.activeToggles = Object.freeze({!! feature_toggle_api()->activeTogglesToJson() !!});
</script>