Super Simple User Based Feature Flags
Through a simple addition to the user model we can enable application features for specific users only
When developing a new feature, it can be very useful to be able to get this feature in the hands of trusted users whilst all other users see nothing different. This type of functionality can be termed a Feature Switch (or feature toggle). The method implemented here is easy to implement and robust in use.
There are only three components to this implementation
    1.
    A config section that indicates if the feature is globally available
    2.
    A function on the User model which tells us if the user has that feature available to them
    3.
    Use of the function in views and classes to guide the workflow or enable view sections
Of course this approach may not suit all, and attention has to be paid to the effect of database changes for the feature which may not be available to all users.

The config section

You could have a dedicated config file for feature switches, but in this case we use the app.php file and add a new section to it;
config/app.php
1
/*
2
| Features
3
| NU = Nudges are enabled
4
*/
5
'features' => [
6
'SE', // Search is enabled
7
'FL', // Flashcards are enabled
8
],
9
Copied!
Features that are globally available (available to all users) are added to the features section. The comment tells us what other features are available but not yet implemented.
You will start out with this features array empty, and instead give the flag to the test users. After you are happy with the new feature and want to roll it out to all users, add the appropriate flag to the features array.

The User model

The addition of a simple function hasFeature will tell us if the feature is turned on for this user or turned on for everyone.
User.php
1
public function hasFeature($code)
2
{
3
// ignores user setting if the feature code is globally enabled.
4
if(collect(config('app.features' ?? [] ))->contains($code)){
5
return true;
6
}
7
8
return !! collect(explode(',',$this->features))->contains($code);
9
}
Copied!
If the global config contains the passed flag then the feature is available, irrespective of what the User model holds. If the value is not in config, then check if the User model contains this value.
A simple string column is added to the users table containing features in this field place a comma separated list of features available to this user, ie 'FL,NU'.

Restricting the use of the feature

In views, a simple @if directive may be used. For example;
1
@if(Auth::user()->hasFeature('SE'))
2
<a href="{{ route('search') }}" class="px-2 py-2 text-sm font-bold text-center">SEARCH</a>
3
@endif
Copied!
In classes and controllers, since the function simply returns true or false, we can use if, case statements, ternary statements etc
1
if($user->hasFeature('NU')){
2
// something to do with feature
3
}
Copied!

Cleaning up afterwards

Once you have proven your new feature and it has been rolled out globally for a period of time, it is best-practice to plan to remove the feature switch from your code now that it is no longer required.
Last modified 8mo ago