To manage feature flags for different user roles using Laravel Pennant, you can leverage its ability to define feature flags with custom logic that evaluates the authenticated user's role or other attributes. Here's a detailed explanation of how to do this:
Defining Feature Flags Based on User Roles
Laravel Pennant allows you to define feature flags using closures that receive the authenticated user as an argument. This means you can write logic that checks the user's role to determine if a feature should be enabled for them.
For example, to enable a feature called `telecommunications` only for users with an admin role, you can define the feature flag like this:
php
use Laravel\Pennant\Feature;
use App\Models\User;
Feature::define('telecommunications', function (User $user): bool {
return $user->isAdmin(); // Assuming isAdmin() checks the user's role
});
This closure returns `true` if the user is an admin, enabling the feature for them, and `false` otherwise[3][4].
You can make the logic more complex if needed, such as enabling a feature for admins, beta testers, or a random subset of users:
php
Feature::define('new-ui', function (User $user): bool {
return $user->isAdmin() || $user->isBetaTester() || rand(1, 100) activate('telecommunications');
Similarly, to deactivate:
php
Feature::for($someUser)->deactivate('telecommunications');
You can also activate or deactivate a feature for everyone:
php
Feature::activateForEveryone('telecommunications');
Feature::deactivateForEveryone('telecommunications');
This flexibility allows you to manage feature access dynamically based on user roles or subscription status[3].
Using the HasFeatures Trait on User Model
Laravel Pennant provides a `HasFeatures` trait that you can add to your `User` model. This trait adds a `features()` method to the user instance, allowing you to check feature flags directly on the user:
php
namespace App\Models;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Laravel\Pennant\Concerns\HasFeatures;
class User extends Authenticatable
{
use HasFeatures;
// ...
}
You can then check a feature flag for any user like this:
php
$user = auth()->user();
if ($user->features()->active('telecommunications')) {
// Feature enabled for this user
}
Or for another user:
php
User::find(1)->features()->active('telecommunications');
This approach keeps your feature flag checks clean and user-centric[3].
Testing Feature Flags with Role-Based Logic
When testing, you can instantiate your feature flag class and call its `resolve` method with different user instances to assert the expected behavior based on roles:
- Admin user should have the feature enabled.
- Beta program users should have the feature enabled.
- Regular users should have the feature disabled.
This isolation helps maintain clean and maintainable test code that focuses on feature flag logic rather than scattered conditional checks[3].
Summary
- Define feature flags with closures that receive the authenticated user and check their role or other attributes.
- Use `Feature::active('feature-name')` to check if a feature is enabled for the current user.
- Use `Feature::for($user)->activate()` or `deactivate()` to toggle feature flags for specific users.
- Use the `HasFeatures` trait on your `User` model for convenient feature flag checks on user instances.
- Test feature flags independently by resolving them with different user roles.
This approach centralizes feature flag logic, making it easier to maintain and evolve your application's feature rollout strategy based on user roles with Laravel Pennant[3][4][6].
Citations:
[1] https://laravel.com/docs/11.x/pennant
[2] https://www.honeybadger.io/blog/a-guide-to-feature-flags-in-laravel/
[3] https://laravel-news.com/feature-flags-with-laravel-pennant
[4] https://dev.to/saurabh-dhariwal/how-to-manage-feature-flags-with-laravel-pennant-in-2024-1phb
[5] https://github.com/stephenjude/filament-feature-flags
[6] https://bmehul.com/articles/laravel-pennant-a-guide-to-feature-flags
[7] https://stackoverflow.com/questions/77751003/laravel-pennant-store-against-session-for-a-b-testing
[8] https://www.reddit.com/r/laravel/comments/10yto7q/laravel_pennant_simple_and_lightweight_feature/