amirhf1 / feature-flags
This package provides a simple and flexible feature flag system for Laravel applications. It allows developers to enable or disable features dynamically, roll out features to a percentage of users, or restrict features to specific users.
Requires
- php: ^7.4 || ^8.0
- illuminate/support: ^8.0 || ^9.0 || ^10.0
Requires (Dev)
- nunomaduro/collision: ^6.4
- orchestra/testbench: ^8.18
- phpunit/phpunit: ^9.5
This package is auto-updated.
Last update: 2025-07-11 13:01:34 UTC
README
This package provides a simple and flexible feature flag system for Laravel applications. It allows developers to dynamically enable or disable features, roll out features to a percentage of users, or restrict features to specific users.
Features
- Toggle Features: Enable or disable features dynamically.
- Percentage Rollout: Roll out features to a percentage of users.
- User-Based Rollout: Enable features only for specific users.
- Database-Backed Flags: Store feature flags in the database.
- Config-Based Flags: Define feature flags directly in the Laravel config file.
- Command-Line Interface (CLI): Enable, disable, or list feature flags from the terminal.
Installation
To install the package, run the following command:
composer require amirhf1/feature-flags
Once installed, you need to register the service provider in your Laravel application. Add the following line to the providers
array in config/app.php
:
Amirhf1\FeatureFlags\Providers\FeatureFlagsServiceProvider::class,
Configuration
The package comes with a default configuration file. To publish the config file to your application, run:
php artisan vendor:publish --provider="Amirhf1\FeatureFlags\Providers\FeatureFlagsServiceProvider" --tag="config"
This will create the config/feature-flags.php
file, where you can define and manage your feature flags.
Example of a feature flag in config/feature-flags.php
:
return [ 'flags' => [ 'new_feature' => [ 'enabled' => true, 'percentage' => 50, // Enable for 50% of users 'users' => [1, 2, 3], // Enable for specific user IDs ], ], ];
Database Migrations
To store feature flags in the database, you need to run the migration command:
php artisan migrate
This will create the feature_flags
table in your database.
To create the migration file, run:
php artisan vendor:publish --provider="Amirhf1\FeatureFlags\Providers\FeatureFlagsServiceProvider" --tag="migrations"
This will copy the migration file to your database/migrations
folder.
Usage
Checking if a Feature is Enabled
You can check if a feature is enabled using the FeatureFlags
class or the helper function.
use Amirhf1\FeatureFlags\Facades\FeatureFlags; if (FeatureFlags::isEnabled('new_feature')) { // The feature is enabled, perform the feature-specific logic }
Or using the helper function:
if (feature_enabled('new_feature')) { // The feature is enabled, perform the feature-specific logic }
Enabling and Disabling Features
You can enable or disable features programmatically or via the command line.
Enable a Feature:
FeatureFlags::enable('new_feature');
Disable a Feature:
FeatureFlags::disable('new_feature');
Command-Line Interface (CLI)
The package provides several Artisan commands to interact with feature flags.
-
List all feature flags:
php artisan feature:list
-
Enable a feature flag:
php artisan feature:enable {feature_name}
-
Disable a feature flag:
php artisan feature:disable {feature_name}
Tests
To run the tests, you need to set up PHPUnit. Then, you can run the tests using:
php artisan test