A lightweight and simple solution to manage feature flags in your Laravel applications.
You can install the package via composer:
composer require evanperreau/laravel-feature-flags-liteAfter installation, publish the configuration file:
php artisan vendor:publish --provider="Evanperreau\LaravelFeatureFlagsLite\FeatureFlagsServiceProvider" --tag="config"This will create a features.php file in your config folder with the following structure:
<?php
return [
/*
* Feature flags
*
* 'feature_name' => true,
*/
];Add your feature flags in the config/features.php configuration file:
<?php
return [
'new_ui' => true,
'beta_feature' => false,
'premium_feature' => true,
];You can check if a feature flag is enabled in three different ways:
use Evanperreau\LaravelFeatureFlagsLite\Facades\Feature;
if (Feature::isEnabled('new_ui')) {
// The 'new_ui' feature flag is enabled
}if (feature('new_ui')) {
// The 'new_ui' feature flag is enabled
}use Evanperreau\LaravelFeatureFlagsLite\FeatureFlags;
class MyController
{
public function index(FeatureFlags $featureFlags)
{
if ($featureFlags->isEnabled('new_ui')) {
// The 'new_ui' feature flag is enabled
}
}
}You can easily use feature flags in your Blade views:
@if(feature('new_ui'))
<div class="new-ui-component">
<!-- Content visible only if the 'new_ui' feature flag is enabled -->
</div>
@else
<div class="old-ui-component">
<!-- Content visible if the 'new_ui' feature flag is disabled -->
</div>
@endifYou can protect routes or groups of routes with the feature flag middleware. If the feature flag is disabled, the request will be aborted with a 404 response by default.
// Only accessible if 'premium_feature' is enabled
Route::get('/premium-content', 'PremiumController@index')
->middleware('feature:premium_feature');// Returns 403 Forbidden if 'beta_feature' is disabled
Route::get('/beta-feature', 'BetaController@index')
->middleware('feature:beta_feature,403');// All routes in this group require 'admin_panel' to be enabled
Route::middleware('feature:admin_panel')->group(function () {
Route::get('/admin/dashboard', 'AdminController@dashboard');
Route::get('/admin/users', 'AdminController@users');
Route::get('/admin/settings', 'AdminController@settings');
});Use descriptive names for your feature flags. For example:
new_user_onboardingpremium_dashboardbeta_reporting_tools
You can use different values for your feature flags depending on the environment by using environment variables in your .env file:
FEATURE_NEW_UI=true
FEATURE_BETA_FEATURE=false
And in your features.php configuration file:
<?php
return [
'new_ui' => env('FEATURE_NEW_UI', false),
'beta_feature' => env('FEATURE_BETA_FEATURE', false),
];Contributions are welcome! Feel free to open an issue or submit a pull request.
This package is open-source and available under the MIT license.