amirhf1/feature-toggle

FeatureFlip is a dynamic Laravel package that simplifies the management of feature toggles, allowing developers to switch functionalities on or off with ease. Enhance your application's flexibility and reduce deployment risks by controlling feature availability without altering the codebase.

v1.0.0 2024-10-15 21:57 UTC

This package is auto-updated.

Last update: 2024-12-15 22:21:45 UTC


README

Packagist License PHP Version Total Downloads

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.

Table of Contents

Features

  • 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.

Installation

You can install the package via Composer:

composer require amirhf1/feature-toggle

Configuration

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.

Usage

Checking Feature Status

Use the FeatureToggle facade to check if a feature is enabled:

use FeatureToggle;

if (FeatureToggle::isEnabled('comments')) {
    // Show comments section
} else {
    // Hide comments section
}

Enabling/Disabling Features

Enable or disable features programmatically:

use FeatureToggle;

// Enable a feature
FeatureToggle::enable('new_dashboard');

// Disable a feature
FeatureToggle::disable('registration');

Middleware

Protect your routes based on feature flags by using the provided middleware.

  1. Register Middleware:

    The middleware is automatically registered by the package. If not, ensure it's registered in your FeatureToggleServiceProvider.

  2. 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.

Blade Directives

Conditionally display content in your Blade templates using the @feature directive.

@feature('comments')
    <!-- Comments section -->
@else
    <!-- Comments are disabled -->
@endfeature

Database Setup

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.

Seeding Default Features

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

Contributing

Contributions are welcome! Please follow these steps to contribute:

  1. Fork the Repository

  2. Create a Feature Branch

    git checkout -b feature/YourFeature
  3. Commit Your Changes

    git commit -m "Add your feature"
  4. Push to the Branch

    git push origin feature/YourFeature
  5. Open a Pull Request

Please ensure your code adheres to the project's coding standards and includes appropriate tests.

License

The MIT License (MIT). Please see LICENSE for more information.