PHP code example of phpdominicana / flagship

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

    

phpdominicana / flagship example snippets


use Flagship\Facades\Flagship;

Flagship::create('new_feature', true);

if (Flagship::isEnabled('new_feature')) {
    // Code for the new feature
}

if (Flagship::isEnabled('beta_feature', auth()->user())) {
    // User-specific feature
}

use PhpDominicana\Flagship\Facades\Flagship;

// Simple feature check
if (Flagship::isEnabled('new-checkout')) {
    // Show new checkout flow
}

// User-specific feature check
if (Flagship::isEnabledForUser('beta-dashboard', $user)) {
    // Show beta dashboard to specific user
}

// Check with fallback
$showFeature = Flagship::isEnabled('experimental-ui', false);

Route::middleware(['flagship:new_feature'])->group(function () {
    Route::get('/new-section', [Controller::class, 'newSection']);
});

Route::middleware(['feature:new-api'])->group(function () {
    Route::get('/api/v2/users', [UserController::class, 'index']);
});

// With user context
Route::middleware(['feature:beta-features,user'])->group(function () {
    Route::get('/beta/dashboard', [BetaController::class, 'dashboard']);
});

use PhpDominicana\Flagship\Models\Feature;

// Create a new feature
Feature::create([
    'name' => 'new-checkout',
    'description' => 'New streamlined checkout process',
    'is_active' => true,
    'rollout_percentage' => 25, // 25% of users
]);

// Target by user attributes
$feature = Feature::create([
    'name' => 'premium-features',
    'targeting_rules' => [
        'user_type' => 'premium',
        'registration_date' => ['after' => '2024-01-01']
    ]
]);

// Target by custom logic
$feature = Feature::create([
    'name' => 'beta-ui',
    'targeting_strategy' => 'custom',
    'custom_evaluator' => BetaUserEvaluator::class
]);

$feature = Feature::create([
    'name' => 'checkout-flow',
    'variants' => [
        'control' => ['weight' => 50],
        'variant_a' => ['weight' => 25],
        'variant_b' => ['weight' => 25]
    ]
]);

// In your code
$variant = Flagship::getVariant('checkout-flow', $user);
switch ($variant) {
    case 'variant_a':
        return view('checkout.variant-a');
    case 'variant_b':
        return view('checkout.variant-b');
    default:
        return view('checkout.control');
}

Feature::create([
    'name' => 'black-friday-sale',
    'scheduled_start' => '2024-11-29 00:00:00',
    'scheduled_end' => '2024-12-02 23:59:59',
]);

Feature::create([
    'name' => 'debug-toolbar',
    'environments' => [
        'local' => true,
        'staging' => true,
        'production' => false
    ]
]);

// Track feature impressions
Flagship::track('new-checkout', $user, 'viewed');

// Track feature interactions
Flagship::track('new-checkout', $user, 'completed_purchase', [
    'amount' => 99.99,
    'items' => 3
]);

// Get feature adoption rates
$stats = Flagship::getFeatureStats('new-checkout');
// Returns: ['impressions' => 1000, 'interactions' => 150, 'conversion_rate' => 15%]

// A/B test results
$results = Flagship::getABTestResults('checkout-flow');

// config/flagship.php

return [
    'cache_enabled' => true,
    'cache_ttl' => 3600,
    'default_state' => false,
    
    // Configure additional middleware for API endpoints
    'api' => [
        'middleware' => ['auth:sanctum'], // Example: Add authentication middleware
    ],
];
bash
composer vendor:publish --provider="Flagship\FlagshipServiceProvider"
php artisan migrate
bash
php artisan flagship:make new_feature --enabled
php artisan flagship:make beta_feature --description="Beta functionality"
bash
  php artisan flagship:list
  
bash
  php artisan flagship:toggle new_feature