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-paced development world, managing how and when new features are released is critical to providing a stable user experience. Feature flags (also called feature toggles) let developers enable or disable features without deploying 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 ways to implement feature flags in Laravel:
A simple approach using configuration files
A more advanced method using the Laravel Pennant package
We’ll also discuss when to choose a package over manual configuration.
Feature flags offer several advantages:
Gradual Rollouts: Release new features to a small percentage of users first, reducing the risk of widespread issues.
A/B Testing: Test multiple versions of a feature to see which performs best.
Quick Rollbacks: Disable problematic features instantly without emergency code deployments.
For small applications or projects with just a few feature flags, using Laravel’s configuration system is the simplest approach.
Create a file config/feature_flags.php:
<?php
return [
'new_feature' => env('FEATURE_NEW_FEATURE', false),
];Add feature flags to your environment file:
FEATURE_NEW_FEATURE=trueUse the flag inside your application logic:
if (config('feature_flags.new_feature')) {
// Code for the new feature goes here
}Pros:
Simple and quick to implement
No extra packages required
Cons:
Can become messy with many flags
No support for user-specific or environment-specific toggles
For complex applications with user-based or environment-specific features, Laravel Pennant is a robust solution.
composer require laravel/pennantphp artisan vendor:publish --provider="Laravel\Pennant\PennantServiceProvider"Inside config/pennant.php, define your features:
<?php
return [
'features' => [
'new_feature' => [
'enabled' => env('FEATURE_NEW_FEATURE', false),
],
],
];use Laravel\Pennant\Feature;
if (Feature::enabled('new_feature')) {
// Code for the new feature
}Enable features only for specific users (e.g., beta testers):
use Laravel\Pennant\Feature;
if (auth()->user()?->hasRole('beta-tester') && Feature::enabled('new_feature')) {
// Feature accessible only to beta testers
}Activate features in certain environments (e.g., staging):
use Laravel\Pennant\Feature;
if (app()->environment('staging') && Feature::enabled('new_feature')) {
// Enabled only in the staging environment
}Use Config Files If:
You have only a few simple feature flags
Flags don’t change often
No user-specific or environment-specific logic is needed
Use Laravel Pennant If:
You need to manage many features
You want per-user or environment-specific toggles
You require maintainable, flexible, and Laravel-native integration
Feature flags are a powerful tool for controlling feature rollouts.
Whether you choose Laravel’s configuration files for simplicity or Laravel Pennant for advanced control, the key is that your team can deliver, test, and refine features safely.
By using feature flags, you can:
Release updates gradually
Respond quickly to issues
Create better, data-driven experiences for your users
Feature flags are toggles that allow developers to enable or disable features without deploying new code.
Use Pennant if you need user-based or environment-based toggles, or if you have many feature flags to manage.
Yes! Feature flags allow you to expose different versions of a feature to users for testing and analysis.
Absolutely. Properly managed feature flags help you gradually roll out features and quickly disable problematic features.
Not necessarily. Pennant supports file-based, database-based, or custom strategies depending on your requirements.