Laravel Integration Guide

Laravel Feature Flags

Feature flags for Laravel — Blade directives, middleware, and safe rollouts

Quick Start

1. Get your API key

Sign up at flagbit.anethoth.com to get your SDK key. The free tier includes 1,000 evaluations/day.

2. Install (optional)

composer require guzzlehttp/guzzle

3. Evaluate flags

<?php
// app/Services/FlagBit.php
namespace App\Services;

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

class FlagBit
{
    public static function enabled(string $flagKey, array $context = []): bool
    {
        return Cache::remember("flag:{$flagKey}:" . md5(json_encode($context)), 60,
            function () use ($flagKey, $context) {
                try {
                    $response = Http::withHeaders([
                        'X-SDK-Key' => config('services.flagbit.sdk_key'),
                    ])->post('https://flagbit.anethoth.com/api/v1/evaluate', [
                        'flag_key' => $flagKey,
                        'context' => $context,
                    ]);
                    return $response->json('value', false);
                } catch (\Exception $e) {
                    return false;
                }
            }
        );
    }
}

// Blade directive — @feature('new-checkout') ... @endfeature
// In AppServiceProvider::boot()
Blade::if('feature', function (string $flagKey) {
    $user = auth()->user();
    $context = $user ? ['user_id' => (string)$user->id, 'plan' => $user->plan] : [];
    return FlagBit::enabled($flagKey, $context);
});

// Middleware
class FeatureFlag
{
    public function handle($request, Closure $next, string $flagKey)
    {
        if (!FlagBit::enabled($flagKey, ['user_id' => auth()->id()])) {
            abort(404);
        }
        return $next($request);
    }
}

// Route usage
Route::get('/beta/dashboard', [BetaController::class, 'index'])
    ->middleware('feature:beta-dashboard');

// Blade usage
@feature('promo-banner')
    <div class="bg-indigo-600 p-4 text-white text-center">
        🎉 New feature available! <a href="/upgrade">Upgrade now</a>
    </div>
@endfeature

Use Cases for Laravel

Blade Templates

Use @feature directives to conditionally render UI components — cleaner than if/else blocks.

Route Middleware

Gate entire routes behind flags — return 404 for users who shouldn't see unreleased features.

Queue Workers

Disable Laravel queue jobs during incidents without touching config or redeploying.

Multi-tenant SaaS

Enable premium features per tenant using context-based targeting rules.

How FlagBit Works

1

Create a Flag

Define flags in your FlagBit dashboard with targeting rules and rollout percentages.

2

Evaluate in Code

Call the evaluate endpoint from your Laravel app with user context for targeted rollouts.

3

Toggle Instantly

Enable, disable, or adjust rollouts in real-time. No redeployment needed.

FAQ

FlagBit is a hosted API — no migrations, no Eloquent models, no maintenance. Pennant stores flags in your database; FlagBit manages everything externally.

The example above uses Laravel Cache with a 60-second TTL. Adjust based on how often flags change.

Mock the FlagBit service in tests: $this->mock(FlagBit::class)->shouldReceive('enabled')->andReturn(true);

Yes — call FlagBit::enabled() in Livewire components just like any PHP code.

Start using feature flags in Laravel

Free tier includes 1 project, 10 flags, and 1,000 evaluations/day. No credit card required.

Get Your Free API Key