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
:
- Create a Service Provider (if you don't have one):
php artisan make:provider FeatureServiceProvider
Define the Feature Flag in the Service Provider:
- Register the Service Provider:
Ensure the service provider is registered in 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:
- 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
});
- Check the Feature Flag in Your Controller:
Use the Feature Flag in Your Blade Template:
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!