PHP code example of mohamedhekal / laravel-featurebox

1. Go to this page and download the library: Download mohamedhekal/laravel-featurebox 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/ */

    

mohamedhekal / laravel-featurebox example snippets


use FeatureBox\Facades\FeatureBox;

if (FeatureBox::isEnabled('new_checkout')) {
    // Show new checkout flow
} else {
    // Show classic checkout
}

FeatureBox::isEnabled('new_checkout', [
    'user_id' => auth()->id(),
    'role'    => auth()->user()->role,
]);

@if(FeatureBox::isEnabled('dark_mode'))
    <link rel="stylesheet" href="/css/dark-mode.css">
@endif

public function checkout()
{
    if (FeatureBox::isEnabled('new_checkout')) {
        return view('checkout.new');
    }
    
    return view('checkout.classic');
}

// Basic check
$enabled = FeatureBox::isEnabled('new_feature');

// With context
$enabled = FeatureBox::isEnabled('premium_feature', [
    'user_id' => 123,
    'plan' => 'premium'
]);

if (FeatureBox::isDisabled('old_feature')) {
    // Show alternative
}

// Enable without conditions
FeatureBox::enable('simple_feature');

// Enable with conditions
FeatureBox::enable('conditional_feature', [
    'environments' => ['production'],
    'user_roles' => ['admin']
]);

FeatureBox::disable('deprecated_feature');

$feature = FeatureBox::get('my_feature');
// Returns: [
//     'name' => 'my_feature',
//     'is_enabled' => true,
//     'conditions' => [...],
//     'created_at' => '2024-01-01 00:00:00',
//     'updated_at' => '2024-01-01 00:00:00'
// ]

$features = FeatureBox::all();
// Returns array of all features

return [
    'cache' => [
        'enabled' => env('FEATUREBOX_CACHE_ENABLED', true),
        'ttl' => env('FEATUREBOX_CACHE_TTL', 300), // 5 minutes
    ],
    
    'default_conditions' => [
        // Default conditions for all features
    ],
    
    'table' => env('FEATUREBOX_TABLE', 'features'),
];

use FeatureBox\Facades\FeatureBox;

public function test_feature_can_be_enabled()
{
    FeatureBox::enable('test_feature');
    
    $this->assertTrue(FeatureBox::isEnabled('test_feature'));
}

// Enable new checkout for premium users only
FeatureBox::enable('new_checkout', [
    'user_roles' => ['premium'],
    'environments' => ['production']
]);

// In your checkout controller
public function checkout()
{
    $user = auth()->user();
    
    if (FeatureBox::isEnabled('new_checkout', [
        'user_id' => $user->id,
        'role' => $user->role
    ])) {
        return $this->newCheckout();
    }
    
    return $this->classicCheckout();
}

// Enable beta features for specific users
FeatureBox::enable('beta_dashboard', [
    'user_ids' => [1, 5, 10, 15],
    'start_date' => '2025-01-01',
    'end_date' => '2025-03-31'
]);

// Enable debug features only in local environment
FeatureBox::enable('debug_panel', [
    'environments' => ['local']
]);

// Roll out to 10% of users
FeatureBox::enable('gradual_rollout', [
    'custom' => [
        'rollout_percentage' => 10
    ]
]);

// In your code
$user = auth()->user();
$rolloutHash = crc32($user->id . 'gradual_rollout');
$inRollout = ($rolloutHash % 100) < 10;

if (FeatureBox::isEnabled('gradual_rollout', [
    'rollout_percentage' => $inRollout ? 10 : 0
])) {
    // New feature
}
bash
# Install the package
composer tions
php artisan vendor:publish --tag=featurebox-config
php artisan vendor:publish --tag=featurebox-migrations

# Run migrations
php artisan migrate

# Enable a feature
php artisan featurebox:enable new_checkout

# Use in your code
if (FeatureBox::isEnabled('new_checkout')) {
    // New checkout flow
}
bash
php artisan vendor:publish --tag=featurebox-config
php artisan vendor:publish --tag=featurebox-migrations
php artisan migrate
bash
php artisan featurebox:disable new_checkout
bash
php artisan featurebox:list