To set up feature definitions and administrators in Laravel Pennant, follow these detailed steps:
1. Installation and Setup
First, install Laravel Pennant via Composer:bash
composer require laravel/pennant
Then publish the configuration and migration files:
bash
php artisan vendor:publish --provider="Laravel\Pennant\PennantServiceProvider"
Run the migrations to create the necessary database tables for persistent feature flag storage:
bash
php artisan migrate
This sets up the foundation for using feature flags with Laravel Pennant[1][9].
2. Defining Feature Flags
Feature flags are defined using the `Feature` facade, typically in a service provider or a dedicated feature flag configuration file. You define a feature flag by giving it a name and optionally a callback that returns a boolean or any value determining if the feature is enabled.Basic definition example:
php
use Laravel\Pennant\Feature;
Feature::define('new-dashboard');
This defines a feature flag called `new-dashboard` which is disabled by default.
You can define feature flags with conditional logic, for example, enabling a feature only for administrators:
php
use Laravel\Pennant\Feature;
use App\Models\User;
Feature::define('telecommunications', function (User $user) {
return $user->isAdmin();
});
Here, the feature `telecommunications` is active only if the authenticated user is an administrator[2][5].
3. Managing Feature Flags for Administrators
To specifically set administrators as the scope for a feature flag, the closure passed to `Feature::define` can check the user's role or permissions. For example:php
Feature::define('admin-only-feature', function (User $user) {
return $user->isAdmin();
});
This ensures that the feature is enabled only for users with admin privileges. You can expand this logic to include other roles or conditions as needed.
4. Activating and Deactivating Features
You can programmatically activate or deactivate features globally or for specific scopes (e.g., teams, users):- Activate a feature globally:
php
Feature::activateForEveryone('new-api');
- Deactivate a feature globally:
php
Feature::deactivateForEveryone('new-api');
- Activate or deactivate a feature for a specific scope (like a team or user):
php
Feature::for($user->team)->activate('billing-v2');
Feature::for($user->team)->deactivate('billing-v2');
You can also activate a feature with a rich value, such as a string:
php
Feature::activate('purchase-button', 'seafoam-green');
To forget a stored feature value so it resolves again from the definition:
php
Feature::forget('purchase-button');
These methods provide flexibility in managing feature flags dynamically[1].
5. Using Feature Flags in Your Application
To check if a feature is active, use:php
if (Feature::active('new-feature')) {
// Show new feature
} else {
// Show old feature
}
This check respects the feature definition and any stored activation states[5].
Summary
- Install Laravel Pennant and run migrations.- Define features using `Feature::define()`, optionally with user-based logic to restrict features to admins.
- Use `Feature::activate()`, `Feature::deactivate()`, and their scoped variants to manage feature states.
- Check feature status in your app with `Feature::active()`.
This approach allows you to create fine-grained feature flags and control access based on administrator roles or other criteria, enabling safe and incremental feature rollouts in Laravel applications[1][2][5].
Citations:
[1] https://laravel.com/docs/11.x/pennant
[2] https://www.honeybadger.io/blog/a-guide-to-feature-flags-in-laravel/
[3] https://redberry.international/laravel-pennant-feature-flagging-made-easy/
[4] https://laravel-news.com/feature-flags-with-laravel-pennant
[5] https://dev.to/saurabh-dhariwal/how-to-manage-feature-flags-with-laravel-pennant-in-2024-1phb
[6] https://bmehul.com/articles/laravel-pennant-a-guide-to-feature-flags
[7] https://www.reddit.com/r/laravel/comments/13htk4p/laravel_pennant_management/
[8] https://www.youtube.com/watch?v=EjLAaeHSPWY
[9] https://wpwebinfotech.com/blog/laravel-pennant-guide/