DevOps

/laravel-agent:feature-flag:make

Create feature flag with Laravel Pennant

Overview

The /feature-flag:make command generates feature flag classes using Laravel Pennant. It supports multiple resolution strategies including simple boolean flags, A/B testing variants, percentage-based rollouts, user segments, and subscription-based features.

Usage

/laravel-agent:feature-flag:make [feature name] [type] [specification]

Examples

# Create a simple boolean feature flag
/laravel-agent:feature-flag:make NewDashboard

# Create an A/B test with variants
/laravel-agent:feature-flag:make PricingVariant ab-test

# Create a user segment-based flag
/laravel-agent:feature-flag:make BetaFeatures segment beta-users

# Create a gradual percentage rollout
/laravel-agent:feature-flag:make GradualRollout percentage 25

Feature Flag Types

The command supports the following feature flag types:

Type Use Case Resolution Logic
boolean Simple on/off toggle Always returns true or false (default)
ab-test A/B/C testing with multiple variants Randomly assigns users to variants
percentage Gradual feature rollout Enables for specified percentage of users
segment User or team-based access Checks if user belongs to segment
subscription Plan-based features Enables based on subscription tier

What Gets Created

The command generates a feature flag class at:

  • app/Features/[FeatureName].php - Feature flag class with resolution logic

Generated Feature Flag Example

For a simple boolean feature flag:

<?php

namespace App\Features;

use Illuminate\Support\Lottery;

class NewDashboard
{
    /**
     * Resolve the feature's initial value.
     */
    public function resolve(User $user): bool
    {
        return true; // or false, or conditional logic
    }
}

Usage Examples

Once generated, you can use feature flags in multiple ways:

In Controllers

<?php

use Laravel\Pennant\Feature;
use App\Features\NewDashboard;

// Check if feature is active
if (Feature::active(NewDashboard::class)) {
    // Show new dashboard
}

// Check if feature is inactive
if (Feature::inactive(NewDashboard::class)) {
    // Show old dashboard
}

In Blade Templates

@feature(App\Features\NewDashboard::class)
    <!-- New dashboard content -->
@else
    <!-- Old dashboard content -->
@endfeature

In Middleware

<?php

Route::get('/dashboard', function () {
    // Dashboard logic
})->middleware('feature:'.NewDashboard::class);

Feature Flag Management

Manage feature flags programmatically:

<?php

use Laravel\Pennant\Feature;
use App\Features\NewDashboard;

// Activate for everyone
Feature::activateForEveryone(NewDashboard::class);

// Deactivate for everyone
Feature::deactivateForEveryone(NewDashboard::class);

// Activate for specific user
Feature::for($user)->activate(NewDashboard::class);

// Deactivate for specific user
Feature::for($user)->deactivate(NewDashboard::class);

// Check current value for user
$isActive = Feature::for($user)->value(NewDashboard::class);

Testing Feature Flags

Test your feature flags with Pest or PHPUnit:

# Run feature flag tests
vendor/bin/pest --filter=NewDashboard
<?php

use Laravel\Pennant\Feature;
use App\Features\NewDashboard;

test('new dashboard is shown when feature is active', function () {
    Feature::activate(NewDashboard::class);

    $response = $this->get('/dashboard');

    $response->assertSee('New Dashboard');
});

test('old dashboard is shown when feature is inactive', function () {
    Feature::deactivate(NewDashboard::class);

    $response = $this->get('/dashboard');

    $response->assertSee('Old Dashboard');
});

Common Use Cases

Command Type Resolution
/feature-flag:make NewUI boolean Always true/false
/feature-flag:make PricingPage ab-test A/B Random variant assignment
/feature-flag:make NewCheckout percentage 10 Percentage 10% of users
/feature-flag:make ProFeatures subscription pro Subscription Pro plan users only

Best Practices

  1. Start with boolean flags - Use simple on/off flags for initial feature development
  2. Use percentage rollouts - Gradually roll out features to monitor performance and user feedback
  3. Clean up old flags - Remove feature flags once features are fully released
  4. Test both states - Always test both enabled and disabled states of feature flags
  5. Document flag purpose - Add comments explaining what each feature flag controls

Related Agent

This command uses the Pennant agent to generate feature flag classes with appropriate resolution logic.

See Also