Blog Detail

05

Aug
Easily Toggle Features using Laravel Feature Flag Package cover image

arrow_back Easily Toggle Features using Laravel Feature Flag Package

Laravel Feature Flag is an awesome package for Laravel using Atriedes/feature library. This package provides the ability to toggle features (by users, groups, random, etc.) using Laravel Authorization.

Installation

You can install this package by using composer:

composer require "friendsofcat/laravel-feature-flag"

Add the following to your config/app.php providers array:

FriendsOfCat\LaravelFeatureFlags\FeatureFlagsProvider::class,

Add the following to your config/app.php providers array:

php artisan vendor:publish --provider="FriendsOfCat\LaravelFeatureFlags\FeatureFlagsProvider" --tag='migrations'

Then run the migration to set up the base table:

php artisan migrate

Usage

Visit /admin/feature_flags to manage features via the UI.

Checking if a feature flag exists

For this you can use the exists() method

if(\FriendsOfCat\LaravelFeatureFlags\Feature::exists('see-twitter-field'))
{
  //do something
}

Enable for User Roles

You can enable a feature flag for specific user roles, by using the roles variant in the configuration form

i.e.

{ "roles": ["admin", "dev"]}

If you don’t have a roles property in your User model, you just need to implement the FeatureFlagsEnabler Interface and use FeatureFlagUserRoleTrait

use FriendsOfCat\LaravelFeatureFlags\FeatureFlagsEnabler;
use FriendsOfCat\LaravelFeatureFlags\FeatureFlagUserRoleTrait;

class User extends Authenticatable implements FeatureFlagsUserRoles
{
    use AuthenticableTrait, FeatureFlagUserRoleTrait;
}

Enable for User Teams

You can enable a feature flag for specific user teams, by using the teams variant in the configuration form

i.e.

{ "teams": ["Team 1", "Team 2"]}

If you don’t have a teams property in your User model, you just need to implement the FeatureFlagsEnabler Interface and use FeatureFlagUserRoleTrait

use FriendsOfCat\LaravelFeatureFlags\FeatureFlagsEnabler;
use FriendsOfCat\LaravelFeatureFlags\FeatureFlagUserRoleTrait;

class User extends Authenticatable implements FeatureFlagsUserRoles
{
    use AuthenticableTrait, FeatureFlagUserRoleTrait;
}

Syncing Flags

Feature flags can be synchronized using the provided feature-flag:sync command. This will sync flags defined in the sync_flags configuration in the laravel-feature-flag.php config file. The format for this flag configuration is key => default value. By default, any flags that are removed will be removed from storage. There is a –skip-cleanup flag available to skip this step.

Demo / Example
If you want to try the demo/example also include the following in your config/app.php providers array:

FriendsOfCat\LaravelFeatureFlags\ExampleFeatureProvider::class

and then run:

php artisan vendor:publish --provider="FriendsOfCat\LaravelFeatureFlags\ExampleFeatureProvider" --tag='migrations'
php artisan migrate

It has a rollback to help clean up after.

There is a dummy route called /admin/feature_flags/example that you can visit and it will show that it is not on. But if you then go to the admin UI /admin/feature_flags you can toggle it on and off.

Closing Note

Codebrisk is here to help you grow your business. Our expert developers have plenty of experience in Laravel Custom Web Application Development. We can enhance the performance of applications with easy syntax, third-party API integrations, and built-in functions, followed by a high level of security to prevent unauthorized access to data. We always provide a customized website solution tailored to our client’s needs. So if you’re interested, you can get in touch with us or get a free quote from us.

Published at : 05-08-2022

Author : Rizwan Aslam
AUTHOR
Rizwan Aslam

I am a highly results-driven professional with 12+ years of collective experience in the grounds of web application development especially in laravel, native android application development in java, and desktop application development in the dot net framework. Now managing a team of expert developers at Codebrisk.

Launch your project

Launch project