Learn how to implement feature flags in Laravel for progressive delivery. This practical guide covers step-by-step implementation and benefits.
In today’s fast-moving development world, managing how and when new features are rolled out is critical to delivering a stable user experience. That’s where feature flags also known as feature toggles come into play. They allow developers to activate or deactivate features without having to push new code. This flexibility is invaluable for progressive delivery, A/B testing, and quick rollbacks when needed.
In this guide, we'll walk through two methods to implement feature flags in Laravel: a simple approach using configuration files, and a more advanced setup with the Laravel Pennant package. We’ll also look at when it makes sense to choose a package over manual configuration.
Feature flags offer a number of important advantages:
Gradual Rollouts: Release a feature to a small percentage of users first, lowering the risk of widespread issues.
A/B Testing: Easily test different versions of a feature to determine which one performs better.
Quick Rollbacks: Instantly disable problematic features without the need for emergency code deployments.
For smaller applications or projects with just a few feature flags, the simplest solution is to manage them via Laravel’s configuration system.
First, create a new file inside your config directory called featured_flags.php
// config/feature_flags.php
return [
'new_feature' => env('FEATURE_NEW_FEATURE', false),
];
Add your feature flags to your environment file:
FEATURE_NEW_FEATURE=true
This setup allows you to turn features on or off without changing any code — just by modifying your .env
file.
Now you can use the feature flag inside your application logic:
if (config('feature_flags.new_feature')) {
// New feature code goes here
}
Pros:
Very simple and fast to implement.
No additional packages required.
Cons:
Managing many flags can quickly become messy.
No support for user-specific or environment-specific rollouts.
For applications that require more complex feature management — like user-specific toggles or environment-based rules — Laravel Pennant is an excellent solution.
Add the Pennant package to your project:
composer require laravel/pennant
Publish the Pennant config file:
php artisan vendor:publish --provider="Laravel\Pennant\PennantServiceProvider"
Inside config/peannat.php, you can define your features.
// config/pennant.php
return [
'features' => [
'new_feature' => [
'enabled' => env('FEATURE_NEW_FEATURE', false),
],
],
];
You can now use Pennant's Feature facade to check flags:
use Laravel\Pennant\Feature;
if (Feature::enabled('new_feature')) {
// Code for the new feature
}
User-Based Flags
You can enable features only for certain users. for example, beta testers:
use Laravel\Pennant\Feature;
if (auth()->user()?->hasRole('beta-tester') && Feature::enabled('new_feature')) {
// Feature is available for beta testers
}
Environment-Specific Features
You might want features active only in specific environments (e.g., staging):
use Laravel\Pennant\Feature;
if (app()->environment('staging') && Feature::enabled('new_feature')) {
// This feature is enabled only in the staging environment
}
Use Laravel Config Files if:
You have just a handful of feature flags.
Your flags are simple and do not change often.
You don't need user-specific or environment-specific logic.
Use Laravel Pennant if:
You have many features to control.
You need per-user or environment-specific toggles.
You want better maintainability, flexibility, and Laravel-native integration.
Feature flags are a smart way to manage how new features reach your users. Whether you stick to Laravel’s basic configuration files for simple needs or leverage a package like Laravel Pennant for more advanced control, the important thing is that you’re giving yourself and your team the ability to deliver, test, and refine features confidently.
By embracing feature flags, you can roll out updates more safely, react quickly to issues, and create better, data-driven experiences for your users.