Introducing Laravel Pennant: Simplify Feature Flag Management

Laravel devs, here's a gem for you: πŸ’Ž

Laravel Pennant is a powerful new tool introduced in Laravel 11 for managing feature flags. It simplifies the process of implementing and maintaining feature toggles, allowing you to test new features and roll them out gradually. In this post, we'll explore how to use Laravel Pennant with a practical example.

What is Laravel Pennant?

Laravel Pennant is a first-party package designed to handle feature flags efficiently. Feature flags are essential for controlling the availability of features in your application, enabling you to test and deploy new functionalities with minimal risk. With Laravel Pennant, you can define, manage, and check feature toggles effortlessly.

Installing Laravel Pennant

First, ensure you have Laravel 11 installed. Then, you can install Laravel Pennant using Composer:

composer require laravel/pennant

Defining Feature Flags

Feature flags in Laravel Pennant are typically defined within a service provider. You can create a new service provider or use an existing one, such as AppServiceProvider. Here’s an example of defining a feature flag called new-color-button:

  1. Create a Service Provider (if you don't have one):
php artisan make:provider FeatureServiceProvider

Define the Feature Flag in the Service Provider:

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Laravel\Pennant\Feature;
use Illuminate\Support\Lottery;

class FeatureServiceProvider extends ServiceProvider
{
    public function boot()
    {
        Feature::define('new-color-button', function () {
            return Lottery::odds(1, 10); // 10% chance of being active
        });
    }

    public function register()
    {
        //
    }
}

FeatureServiceProvide.php

  1. Register the Service Provider:

Ensure the service provider is registered in config/app.php:

'providers' => [
    // Other Service Providers
    App\Providers\FeatureServiceProvider::class,
],

config/app.php

Checking Feature Flags

To check if a feature is active, use the Feature::active method. This method returns a boolean indicating whether the feature is enabled for the current user:

use Laravel\Pennant\Feature;

if (Feature::active('new-color-button')) {
    // Feature is active, execute the new feature code
} else {
    // Feature is not active, execute the fallback code
}

Using Feature Flags in Blade Templates

Laravel Pennant integrates seamlessly with Blade templates, allowing you to conditionally display content based on feature flags:

@feature('new-color-button')
    <!-- HTML for the new color button -->
    <button class="new-color">Click Me!</button>
@else
    <!-- Fallback HTML -->
    <button class="default-color">Click Me!</button>
@endfeature

Example: Gradual Feature Rollout

Let's put it all together in a practical example. Suppose you want to gradually roll out a new button color to your users. Here's how you can do it:

  1. Define the Feature Flag:
use Laravel\Pennant\Feature;
use Illuminate\Support\Lottery;

Feature::define('new-color-button', function () {
    return Lottery::odds(1, 5); // 20% chance of being active
});
  1. Check the Feature Flag in Your Controller:
use Laravel\Pennant\Feature;
use App\Http\Controllers\Controller;

class ButtonController extends Controller
{
    public function show()
    {
        if (Feature::active('new-color-button')) {
            return view('button.new-color');
        } else {
            return view('button.default-color');
        }
    }
}

ButtonController.php

Use the Feature Flag in Your Blade Template:

<!-- resources/views/button.blade.php -->
@feature('new-color-button')
    <button class="new-color">Click Me!</button>
@else
    <button class="default-color">Click Me!</button>
@endfeature

button.blade.php

Conclusion

Integrating Laravel Pennant into your feature management workflow can significantly enhance the flexibility and control you have over feature deployment. By using feature flags, you can ensure a smooth and controlled rollout of new functionalities, improving your development workflow and user experience.

For more detailed guidance, refer to the official Laravel documentation and explore additional resources from the Laravel community.

Found this helpful?

If this guide was helpful to you, subscribe to my daily newsletter and give me a follow on X/Twitter. It helps a lot!

Subscribe to Harris Raftopoulos

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
jamie@example.com
Subscribe