Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon What are the steps to configure Laravel Pennant for a SaaS application


What are the steps to configure Laravel Pennant for a SaaS application


To configure Laravel Pennant for a SaaS application, follow these detailed steps:

1. Install Laravel Pennant Package

Begin by installing the Laravel Pennant package via Composer in your Laravel project root:

bash
composer require laravel/pennant

This adds the feature flag functionality to your application[2][3][5].

2. Publish Configuration and Migration Files

Next, publish Pennant’s configuration and migration files using Artisan:

bash
php artisan vendor:publish --provider="Laravel\Pennant\PennantServiceProvider"

This creates the `config/pennant.php` configuration file and a migration file that will create a `features` table in your database[2][3][5].

3. Run Database Migrations

Run the migrations to create the necessary database tables for persistent feature flag storage:

bash
php artisan migrate

This sets up the `features` table used by Pennant’s default database driver[2][3][5].

4. Define Feature Flags

Feature flags are defined using the `Feature::define()` method, typically in a service provider such as `AppServiceProvider`. Each feature is given a name and a closure that returns a boolean indicating if the feature is active for a given scope (usually the authenticated user).

Example defining a feature flag for a new dashboard available only to beta testers:

php
use Laravel\Pennant\Feature;

public function boot(): void
{
    Feature::define('new-dashboard', function ($user) {
        return $user->isBetaTester();
    });
}

This closure receives the current user by default, allowing you to implement custom logic based on user roles, subscription plans, or other criteria relevant to your SaaS application[2][3][5].

5. Set Default Scope (Optional)

If your SaaS app organizes users into teams or organizations, you can customize the default scope for feature checks. For example, to scope feature flags to the authenticated user's team:

php
use Laravel\Pennant\Feature;
use Illuminate\Support\Facades\Auth;

public function boot(): void
{
    Feature::resolveScopeUsing(fn ($driver) => Auth::user()?->team);
}

This means feature checks will automatically use the user's team as the scope unless explicitly overridden[2].

6. Use Feature Flags in Code and Views

In your controllers, check if a feature is active for the current user or scope:

php
use Laravel\Pennant\Feature;

public function showDashboard()
{
    if (Feature::for(auth()->user())->active('new-dashboard')) {
        return view('dashboard.new');
    }
    return view('dashboard.old');
}

In Blade views, you can use the `@feature` directive to conditionally show content:

blade
@feature('new-dashboard')
    @include('dashboard.partials.new-ui')
@elsefeature
    @include('dashboard.partials.old-ui')
@endfeature

This allows you to progressively roll out new features or UI components to selected users[1][5].

7. Activate or Deactivate Features Dynamically

You can programmatically activate or deactivate features for specific users or scopes, which is useful in SaaS for controlling feature access per subscription tier or user role:

php
Feature::for($user)->activate('new-dashboard');
Feature::for($user)->deactivate('new-dashboard');

This persists the feature flag state in the database if using the default driver[5].

8. Protect Routes with Middleware (Optional)

To restrict access to routes based on feature flags, create middleware that checks if a feature is active:

php
use Laravel\Pennant\Feature;

public function handle($request, Closure $next)
{
    if (Feature::for(auth()->user())->active('new-dashboard')) {
        return $next($request);
    }
    return redirect('/dashboard');
}

Attach this middleware to routes that should only be accessible when the feature is enabled for the user[5].

9. (Optional) Implement Custom Drivers

If your SaaS app requires a custom storage backend (e.g., Redis or a third-party feature flag service), you can implement a custom Pennant driver by creating a class that implements `Laravel\Pennant\Contracts\Driver` and registering it in a service provider:

php
Feature::extend('redis', function ($app) {
    return new RedisFeatureDriver($app->make('redis'), $app->make('events'), []);
});

Then configure Pennant to use your custom driver in `config/pennant.php`[2].

By following these steps, you can effectively configure Laravel Pennant in your SaaS application to manage feature flags, enabling controlled feature rollouts, A/B testing, and user-specific feature access with minimal setup and powerful flexibility.

Citations:
[1] https://laravel-news.com/feature-flags-with-laravel-pennant
[2] https://laravel.com/docs/11.x/pennant
[3] https://www.honeybadger.io/blog/a-guide-to-feature-flags-in-laravel/
[4] https://laravel-news.com/index.php/feature-flags-with-laravel-pennant
[5] https://wpwebinfotech.com/blog/laravel-pennant-guide/
[6] https://laravel-news.com/laravel-pennant
[7] https://bmehul.com/articles/laravel-pennant-a-guide-to-feature-flags
[8] https://elliotderhay.com/blog/how-to-make-laravel-pennant-features-that-apply-globally