PHP code example of j-webb / laravel-unleash
1. Go to this page and download the library: Download j-webb/laravel-unleash 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/ */
j-webb / laravel-unleash example snippets
#/app/Http/Kernel.php
protected $routeMiddleware = [
...
'feature' => \JWebb\Unleash\Middleware\CheckFeature::class,
...
];
public function __construct()
{
$this->middleware('feature:your_feature_name');
}
if (Unleash::isEnabled('your_feature')) {
// Your feature is enabled
}
// List of all features, enabled or disabled
$allFeatures = Unleash::getFeatures();
Result: [
'toggles' => [
'feature_1' => [
'enabled' => true,
'name' => 'feature_1'
],
'feature_2' => [
'enabled' => false,
'name' => 'feature_2'
]
]
]
// List of all enabled features
$enabledFeatures = Unleash::getFeatures(true);
Result: [
'toggles' => [
'feature_1' => [
'enabled' => true,
'name' => 'feature_1'
]
]
]
bash
php artisan vendor:publish --provider="JWebb\Unleash\Providers\ServiceProvider"
php
class ExampleController extends Controller
{
public function __construct()
{
...
$this->middleware('feature:your_feature');
}
}
php
Route::get('/', function () {
//
})->middleware('feature:your_feature');
php
$context = (new UnleashContext())
->setCurrentUserId('some-user-id-from-app')
->setIpAddress('127.0.0.1')
->setSessionId('sess-123456');
$enabled = Unleash::isEnabled('some-feature', $context);
php
$variant = $unleash->getVariant('nonexistentFeature');
assert($variant->isEnabled() === false);