Implementing Feature Flags in Laravel: A Practical Guide to Progressive Delivery

Learn how to implement feature flags in Laravel for progressive delivery. This practical guide covers step-by-step implementation and benefits.

Muhammad Ishaq
Muhammad Ishaq
28 Apr 2025
4 min read
Implementing Feature Flags in Laravel: A Practical Guide to Progressive Delivery

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:

  1. A simple approach using configuration files

  2. A more advanced method using the Laravel Pennant package

We’ll also discuss when to choose a package over manual configuration.


Why Use Feature Flags?

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.


Basic Approach: Using Laravel Config Files

For small applications or projects with just a few feature flags, using Laravel’s configuration system is the simplest approach.

Step 1: Create a Configuration File

Create a file config/feature_flags.php:

<?php

return [
    'new_feature' => env('FEATURE_NEW_FEATURE', false),
];

Step 2: Define Flags in .env

Add feature flags to your environment file:

FEATURE_NEW_FEATURE=true

Step 3: Check Feature Flags in Code

Use 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


Advanced Approach: Using Laravel Pennant

For complex applications with user-based or environment-specific features, Laravel Pennant is a robust solution.

Step 1: Install Laravel Pennant

composer require laravel/pennant

Step 2: Publish Configuration

php 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),
        ],
    ],
];

Step 3: Use Feature Flags in Your Application

use Laravel\Pennant\Feature;

if (Feature::enabled('new_feature')) {
    // Code for the new feature
}

Going Further: Advanced Use Cases

User-Based Flags

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
}

Environment-Specific Features

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
}

When to Use Config Files vs Laravel Pennant

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


Final Thoughts

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


FAQ: Feature Flags in Laravel

1. What are feature flags?

Feature flags are toggles that allow developers to enable or disable features without deploying new code.

2. When should I use Laravel Pennant instead of config files?

Use Pennant if you need user-based or environment-based toggles, or if you have many feature flags to manage.

3. Can I do A/B testing with feature flags?

Yes! Feature flags allow you to expose different versions of a feature to users for testing and analysis.

4. Can feature flags be used in production safely?

Absolutely. Properly managed feature flags help you gradually roll out features and quickly disable problematic features.

5. Do I need a database to use Pennant?

Not necessarily. Pennant supports file-based, database-based, or custom strategies depending on your requirements.