The Laravel Pennant

the laravel pennant

Laravel Pennant is a package for Laravel applications that provides a simple and elegant solution for managing feature flags. Feature flags allow you to control the availability of features in your application, allowing you to selectively release new features to subsets of users or to test new features with a small group of users before releasing them to everyone.

With Laravel Pennant, you can define feature flags in code and then use the provided API to manage the state of those flags. The package includes a database driver for storing feature flag state, but you can also implement your own custom drivers if needed.

In addition to managing feature flags, Laravel Pennant also includes support for A/B testing, allowing you to test different variations of a feature with different groups of users.

Overall, Laravel Pennant provides a powerful and flexible feature flagging solution that can help you manage the complexity of modern web applications and provide a better user experience for your users.

Let’s demonstrate how to use Laravel Pennant in a Laravel 10 application.

  1. Install Laravel Pennant using Composer:
composer require laravel/pennant
    1. Publish the Pennant configuration and migration files:
    php artisan vendor:publish --provider="Laravel\Pennant\PennantServiceProvider"
    1. Run database migrations:
    php artisan migrate
    1. Define a feature in a service provider. For example, let’s create a feature called “new-design” that will randomly assign the new design to 1% of users:

    // app/Providers/AppServiceProvider.php

    use Illuminate\Support\Lottery;
    use Laravel\Pennant\Feature;
    use App\Models\User;
    
    public function boot()
    {
    Feature::define('new-design', function (User $user) {
    return Lottery::odds(1, 100);
    });
    }
    1. Check if a feature is active in a blade template:

    // resources/views/welcome.blade.php 

    @extends('layouts.app')
    
    @section('content')
    <div class="container">
    <div class="row justify-content-center">
    <div class="col-md-8">
    <div class="card">
    <div class="card-header">Dashboard</div>
    
    <div class="card-body">
    @if(feature('new-design'))
    <h1>Welcome to our new design!</h1>
    @else
    <h1>Welcome back!</h1>
    @endif
    </div>
    </div>
    </div>
    </div>
    </div>
    @endsection

    Here, we’re using the feature helper function to check if the “new-design” feature is active. If it is, we display a message welcoming the user to the new design. Otherwise, we display a generic welcome message.

    Note that the feature helper function is provided by Laravel Pennant and takes the feature name as its argument.

    That’s it! With these simple steps, you can start using Laravel Pennant to incrementally roll out new application features with confidence, A/B test new interface designs, and much more.

      Sign up for free tutorials in your inbox.

      We don’t spam! Read our privacy policy for more info.

      Leave a Comment

      Your email address will not be published. Required fields are marked *