php
36 lines · 6 steps
Defining feature flags with Laravel Pennant
A service provider registers feature flags at boot, from simple config toggles to per-user gradual rollouts.
Explained by
highlit
1<?php
2
3namespace App\Providers;
4
5use App\Models\User;
6use Illuminate\Support\Lottery;
7use Illuminate\Support\ServiceProvider;
8use Laravel\Pennant\Feature;
9
10class FeatureServiceProvider extends ServiceProvider
11{
12 public function boot(): void
13 {
14 foreach (config('features.simple', []) as $feature => $enabled) {
15 Feature::define($feature, fn () => (bool) $enabled);
16 }
17
18 Feature::define('new-billing', function (User $user) {
19 if (in_array($user->email, config('features.new_billing.allowlist', []))) {
20 return true;
21 }
22
23 return match (true) {
24 ! config('features.new_billing.enabled') => false,
25 $user->onTrial() => false,
26 default => Lottery::odds(
27 config('features.new_billing.rollout_percent', 0),
28 100,
29 ),
30 };
31 });
32
33 Feature::define('beta-dashboard', fn (User $user) => $user->hasVerifiedEmail()
34 && $user->created_at->lt(config('features.beta_dashboard.cutoff')));
35 }
36}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Feature flags belong in a provider's boot method so they're registered once and resolved lazily on demand.
- 2Driving flag values from config lets you flip features without touching code or redeploying.
- 3Percentage rollouts and allowlists let you ship risky features to a controlled slice of users first.
Related explainers
php
final class Container { private array $bindings = []; private array $instances = [];
Building a DI container with reflection in PHP
dependency-injection
reflection
autowiring
Advanced
9 steps
php
<?php namespace App\Http\Controllers;
Email confirmation with signed URLs in Laravel
signed-urls
email-verification
middleware
Intermediate
6 steps
php
<?php namespace App\RateLimiting;
Sliding-window rate limiting with Redis sorted sets
rate-limiting
redis
sorted-sets
Advanced
8 steps
php
<?php namespace App\Models\Concerns;
How an Auditable trait logs Eloquent changes in Laravel
traits
model-events
polymorphic-relations
Intermediate
8 steps
php
<?php namespace App\Http\Controllers;
Server-Sent Events in Laravel
server-sent-events
streaming
long-polling
Advanced
9 steps
php
<?php namespace App\Actions\Imports;
Streaming CSV imports with Laravel batches
lazy-collections
job-batching
streaming
Advanced
9 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/defining-feature-flags-with-laravel-pennant-explained-php-be9b/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.