🤹
TALL Stack Tips
  • What is TALL Stack
  • Tailwind
    • Tailwind Resources
    • Swinging Bell Notification Icon
    • Styled Unordered Lists
  • Alpine
  • Alpine Resources
  • Tabbed Content Using Alpine JS
  • Checkbox component with SVG tick
  • Dropdown animation
  • Create a Sliding Puzzle Captcha
  • Laravel
    • Tabler Icons Component
    • Password-less Login with Laravel 8+
    • Password-less Login with Magic Link in Laravel 8
    • Laravel Resources
    • Laravel Breeze Login Conditional Redirect
    • Jetstream Login Conditional Redirect
    • Simplify Laravel CRUD Controllers
    • CSRF and expired login forms
    • CSRF and expired logout forms
    • Add your own logo to Laravel Mail
    • Specify a different mail theme for Notifications
    • Show custom page when email verification link expired
    • Using a mutator to save currency
    • Using Spatie Valuestore to hold frequently accessed settings
    • Using the old() helper
    • Alternatives to using Eloquent Accessor
    • UpdateOrCreate may not update timestamp
    • Use of lockForUpdate()
    • Using S3
    • Super Simple User Based Feature Flags
    • Installing a Specific Version of Laravel
    • Versioning your Laravel Project
    • CSS Cache Busting with your Git Commit SHA
    • Adding column to Database Notifications table
    • Find nearby locations using the Haversine formula in Eloquent query
    • Using Queues on Shared Hosting with Laravel
    • Create Guaranteed Unique Invoice Number in Laravel
    • Send Notification to all team members
    • Protect Staging site with Basic Auth
    • Working with Enums
    • PHP DateTime formatting cribsheet
  • Livewire
    • Livewire Resources
    • Naming Livewire Components
    • Dynamic Cascading Dropdown with Livewire
    • Hiding a button after click
    • Working with Javascript Components
    • SweetAlert2 with Livewire
    • Select Multiple or Checkboxes
    • Clearing checkboxes in Livewire
    • Livewire File Uploads Using S3
    • Simple Log File Viewer
  • Related Resources
    • Testing resources
    • When Composer runs out of memory
    • Deployment
    • Security
    • Scheduler & Cron tips
    • LastPass tips
    • Using Git
    • VSCode Tips
    • Markdown
    • Cpanel resources
Powered by GitBook
On this page
  • The config section
  • The User model
  • Restricting the use of the feature
  • Cleaning up afterwards

Was this helpful?

  1. Laravel

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
    /*
    | Features
    | NU = Nudges are enabled
    */
    'features' => [
        'SE',  // Search is enabled
        'FL',  // Flashcards are enabled
    ],
      

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
    public function hasFeature($code)
    {
        // ignores user setting if the feature code is globally enabled.
        if(collect(config('app.features' ?? [] ))->contains($code)){
            return true;
        }

        return !! collect(explode(',',$this->features))->contains($code);
    }

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;

@if(Auth::user()->hasFeature('SE'))
    <a href="{{ route('search') }}" class="px-2 py-2 text-sm font-bold text-center">SEARCH</a>
@endif

In classes and controllers, since the function simply returns true or false, we can use if, case statements, ternary statements etc

if($user->hasFeature('NU')){
  // something to do with feature
}

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.

PreviousUsing S3NextInstalling a Specific Version of Laravel

Last updated 4 years ago

Was this helpful?