Introduction
Feature flagging is a powerful technique that allows developers to enable or disable specific features in an application without deploying new code. It facilitates gradual rollouts, A/B testing, and controlled experiments, ensuring that new features do not disrupt the user experience.
Laravel Pennant, introduced in Laravel 10.16, provides an elegant solution for managing feature flags within Laravel applications. It offers flexible storage mechanisms, dynamic feature activation, and a seamless developer experience, making it an essential tool for modern software development.
In this blog, we will explore:
✔ What Laravel Pennant is and why it matters
✔ How to set up feature flags in Laravel
✔ Best practices for managing feature rollouts
What is Laravel Pennant?
Laravel Pennant is a lightweight feature flagging system that integrates directly with Laravel, allowing developers to control feature availability dynamically. It supports per-user flags, persistent storage options, and efficient performance optimizations.
Key Benefits of Pennant:
✅ Granular Control – Define flags based on user roles or conditions
✅ Flexible Storage – Supports in-memory, database, and cache storage
✅ Seamless Integration – Works effortlessly with Laravel’s core features
✅ Zero Downtime Deployment – Enable/disable features without redeploying
By leveraging Pennant, developers can launch new features incrementally, reducing risk and improving the overall stability of their applications.
Setting Up Laravel Pennant
Step 1: Install Laravel Pennant
Pennant comes pre-installed with Laravel 10.16+, but if needed, install it using Composer:
composer require laravel/pennant
composer require laravel/pennant
After installation, publish the configuration file:
php artisan vendor:publish --tag=pennant-config
Step 2: Define Feature Flags
Feature flags are defined using Laravel’s Feature
facade. Here’s how to set up a simple flag:
use Laravel\Pennant\Feature;
Feature::define('new_dashboard', function ($user) {
return $user->is_admin; // Enable for admins only
});
Step 3: Checking Feature Availability
To check if a feature is active, use the Feature::active()
method:
if (Feature::active('new_dashboard')) {
return view('dashboard.new');
} else {
return view('dashboard.old');
}
To check feature availability for a specific user:
if (Feature::for($user)->active('new_dashboard')) {
return "User has access to the new dashboard";
}
Using Persistent Storage for Feature Flags
Pennant stores flags in memory by default, but for persistence, configure the database storage option.
Step 4: Enable Database Storage
Modify config/pennant.php
:
return [
'store' => 'database',
];
Run the necessary migrations:
php artisan pennant:table
php artisan migrate
Now, feature flags will be stored persistently in the database rather than being reset on every request.
Dynamic Feature Toggles & Rollouts
Feature flags aren’t just for enabling or disabling a feature—they allow fine-tuned control over deployments.
✅ Gradual Feature Rollouts
Enable new features for only 10% of users before a full rollout:
Feature::define('ai-chat', function ($user) {
return $user->id % 10 === 0; // Every 10th user gets access
});
✅ A/B Testing with Feature Flags
Test multiple variations of a feature before making a final decision:
Feature::define('dark_mode', fn($user) => $user->preferences->dark_mode);
✅ Subscription-Based Access Control
Restrict premium features to subscribed users only:
Feature::define('pro_reports', fn($user) => $user->is_subscribed);
Best Practices for Feature Flagging
✔ Always define a fallback option for feature rollbacks
✔ Store flags persistently for large-scale applications
✔ Use caching to improve performance when checking flags frequently
✔ Gradually roll out changes instead of enabling features for all users instantly
Conclusion
Laravel Pennant streamlines feature flagging, providing a scalable, flexible solution for managing feature rollouts. Whether you’re implementing A/B testing, gradual deployments, or subscription-based access, Pennant ensures controlled and efficient feature management without disrupting application stability.
By leveraging Laravel Pennant, developers can build more adaptive, future-proof applications while enhancing user experience through smart feature releases.