filipefernandes / laravel-feature-flags
Reusable Laravel feature flag system
v0.0.2
2025-07-16 11:09 UTC
Requires
- php: ^8.1
- illuminate/support: ^10.0
Requires (Dev)
- inertiajs/inertia-laravel: ^2.0
- larastan/larastan: ^2.11
- laravel/pint: ^1.20
- livewire/livewire: ^3.6
- orchestra/testbench: ^8.0
- pestphp/pest: ^2.0
Suggests
- inertiajs/inertia-laravel: Required for Inertia support
- livewire/livewire: Required for Livewire directives
README
A robust, extensible, and testable feature flag system for Laravel 10+ with Vue/SPA frontend support. Designed as a reusable package for modern Laravel apps.
✨ Features
- ✅ Configurable flags (in config file or database)
- ✅ Boolean or closure-based evaluation
- ✅ Environment-specific overrides
- ✅ Persisted flags in DB (with optional validation)
- ✅ Blade directive:
@feature('flag')
- ✅ Livewire & Inertia integration
- ✅ Middleware for routes or groups
- ✅ Artisan commands to list and manage flags
- ✅ Ui for managing feature flags and history
- ✅ Built with testability & reusability in mind
📦 Installation
composer require filipefernandes/laravel-feature-flags
1. Publish config and migration
php artisan feature-flag:install
⚙️ Configuration
The main config lives in:
config/feature-flags.php
Example:
return [ 'flags' => [ 'flag_1' => [ 'enabled' => true, 'closure' => fn () => auth()->check() && auth()->user()->is_beta, ], 'flag_2' => [ 'enabled' => [ 'dev' => true, 'prod' => false ], ], ], 'ui' => [ 'enabled' => true, 'middleware' => [], 'route_prefix' => 'admin/flags', ], ];
You can define static booleans or closures. If the flag is enabled in DB and has a closure, it will be resolved dynamically.
🧪 Usage
✅ Check if a flag is enabled
use FeatureFlags; // Simple check if a feature flag is enabled for the current user and environment if (FeatureFlags::isEnabled('new_dashboard')) { // Show the new dashboard }
🔄 Check if a flag is enabled with a custom context and environment
$user = User::find(123); $environment = 'staging'; if (FeatureFlags::isEnabled('beta_feature', $user, $environment)) { // Enable beta feature for this user in staging environment }
⚙️ Check a flag with a custom callback
FeatureFlags::isEnabled('complex_feature', null, null, function ($user) { return $user->isAdmin() && $user->created_at->diffInDays(now()) > 30; });
📋 Check multiple flags
// Check if all flags are enabled $allEnabled = FeatureFlags::allAreEnabled(['feature_a', 'feature_b']); // Check if some flags are enabled $someEnabled = FeatureFlags::someAreEnabled(['feature_c', 'feature_d']);
📋 Get all active (enabled) feature flags
Retrieve an array of all currently enabled feature flags, considering both the database and config settings.
use FeatureFlags; // Get all active flags for the current environment (default) $activeFlags = FeatureFlags::all(); // Get all active flags for a specific environment (e.g., 'staging', 'production') $activeFlagsInStaging = FeatureFlags::all('staging');
⚙️ Behavior notes
- By default, the method uses the current app environment (
app()->environment()
). - If your
feature-flags.environments
config is defined and non-empty, the method respects environment-specific overrides on each flag. - If the global environments config is empty or missing, environment checks are ignored, and only the flag’s
enabled
status is used. - Flags defined in the database override config flags.
- The result is cached for the time setting the config file for performance.
🔄 Clear cache
FeatureFlags::clearCache();
✅ Blade directive
@feature('new_dashboard') <x-new-dashboard /> @else <x-old-dashboard /> @endfeature
✅ Inertia support
featureFlags
are automatically shared with frontend (if configured):
import { usePage } from "@inertiajs/vue3"; const featureFlags = usePage().props.featureFlags;
✅ Livewire support
Available in views and component logic:
if (FeatureFlags::isEnabled('experimental_widget')) { // ... }
✅ Middleware
Apply to a route or group:
Route::middleware(['feature:some_flag'])->get('/beta', fn () => 'Beta content');
🛠 CLI Commands
List all feature flags
php artisan feature:flags
Enable/disable a flag
php artisan feature:enable new_dashboard php artisan feature:disable new_dashboard
✅ Requirements
- PHP 8.1+
- Laravel 10+
- Optional: Livewire, Inertia
📜 License
MIT © Filipe Fernandes