FeatureToggle is a Laravel package that provides a simple and flexible way to manage feature flags within your application. Enable or disable features on the fly without deploying new code, enhancing your application's flexibility and allowing for better control over feature releases.
- Easy Integration: Seamlessly integrates with Laravel applications.
- Database-Driven: Store feature flags in the database for dynamic control.
- Facade & Helper Functions: Access feature toggles effortlessly throughout your application.
- Middleware Support: Protect routes based on feature flags.
- Blade Directives: Conditionally display content in Blade templates.
- Extensible: Easily extendable to fit your application's needs.
You can install the package via Composer:
composer require amirhf1/feature-toggle
After installation, publish the configuration and migration files:
php artisan vendor:publish --provider="amirhf1\FeatureToggle\FeatureToggleServiceProvider" --tag="config"
php artisan vendor:publish --provider="amirhf1\FeatureToggle\FeatureToggleServiceProvider" --tag="migrations"
Then, run the migrations to create the features
table:
php artisan migrate
Note: For Laravel versions >=5.5, the package uses Package Auto-Discovery, so manual registration of the service provider and facade is not required. For older versions, refer to the Installation section.
Use the FeatureToggle
facade to check if a feature is enabled:
use FeatureToggle;
if (FeatureToggle::isEnabled('comments')) {
// Show comments section
} else {
// Hide comments section
}
Enable or disable features programmatically:
use FeatureToggle;
// Enable a feature
FeatureToggle::enable('new_dashboard');
// Disable a feature
FeatureToggle::disable('registration');
Protect your routes based on feature flags by using the provided middleware.
-
Register Middleware:
The middleware is automatically registered by the package. If not, ensure it's registered in your
FeatureToggleServiceProvider
. -
Apply Middleware to Routes:
Route::get('/new-dashboard', function () { // New Dashboard Logic })->middleware('feature:new_dashboard');
If the feature is disabled, accessing
/new-dashboard
will result in a 404 error or a custom response as defined.
Conditionally display content in your Blade templates using the @feature
directive.
@feature('comments')
<!-- Comments section -->
@else
<!-- Comments are disabled -->
@endfeature
The package uses a features
table to store feature flags. After publishing and running the migrations, you can manage features via Eloquent models or directly through the database.
Optionally, you can create a seeder to populate default features:
<?php
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
class FeatureToggleSeeder extends Seeder
{
public function run()
{
DB::table('features')->insert([
['name' => 'registration', 'enabled' => true, 'created_at' => now(), 'updated_at' => now()],
['name' => 'comments', 'enabled' => false, 'created_at' => now(), 'updated_at' => now()],
['name' => 'new_dashboard', 'enabled' => false, 'created_at' => now(), 'updated_at' => now()],
// Add more features as needed
]);
}
}
Run the seeder:
php artisan db:seed --class=FeatureToggleSeeder
Contributions are welcome! Please follow these steps to contribute:
-
Fork the Repository
-
Create a Feature Branch
git checkout -b feature/YourFeature
-
Commit Your Changes
git commit -m "Add your feature"
-
Push to the Branch
git push origin feature/YourFeature
-
Open a Pull Request
Please ensure your code adheres to the project's coding standards and includes appropriate tests.
The MIT License (MIT). Please see LICENSE for more information.