Roll Out Features Gradually with Laravel Pennant
Imagine you've developed a new dashboard for your application.
You don't want to release it to every user immediately. Instead, you want to enable it for your internal team first, test it, and gradually make it available to other users.
Laravel Pennant makes this possible through feature flags.
Install Laravel Pennant
Pennant is an official Laravel package, but it needs to be installed separately.
composer require laravel/pennant
Publish its configuration and migration files:
php artisan vendor:publish --provider="Laravel\Pennant\PennantServiceProvider"
php artisan migrate
Define a Feature
You can define features in a service provider:
use Laravel\Pennant\Feature;
public function boot(): void
{
Feature::define('new-dashboard', function ($user) {
return $user->is_admin;
});
}
In this example, the new dashboard is enabled only for administrators.
Check Whether a Feature Is Active
Inside your controller:
use Laravel\Pennant\Feature;
public function index()
{
if (Feature::active('new-dashboard')) {
return view('dashboard.new');
}
return view('dashboard.old');
}
Laravel checks the feature against the currently authenticated user by default.
Enable Features for Specific Users
You can also check a feature for an individual user:
Feature::for($user)->active('new-dashboard');
Or manually activate it:
Feature::for($user)->activate('new-dashboard');
This is useful when granting beta access to selected customers.
Use Feature Flags in Blade
Pennant also provides Blade directives:
@feature('new-dashboard')
<a href="{{ route('dashboard.new') }}">
New Dashboard
</a>
@else
<a href="{{ route('dashboard.old') }}">
Dashboard
</a>
@endfeature
You can control which interface users see without duplicating entire pages.