Feature flags for Laravel — Blade directives, middleware, and safe rollouts
Sign up at flagbit.anethoth.com to get your SDK key. The free tier includes 1,000 evaluations/day.
composer require guzzlehttp/guzzle
<?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 @feature directives to conditionally render UI components — cleaner than if/else blocks.
Gate entire routes behind flags — return 404 for users who shouldn't see unreleased features.
Disable Laravel queue jobs during incidents without touching config or redeploying.
Enable premium features per tenant using context-based targeting rules.
Define flags in your FlagBit dashboard with targeting rules and rollout percentages.
Call the evaluate endpoint from your Laravel app with user context for targeted rollouts.
Enable, disable, or adjust rollouts in real-time. No redeployment needed.
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.
Free tier includes 1 project, 10 flags, and 1,000 evaluations/day. No credit card required.
Get Your Free API Key