jtgrimes/laravel-simple-featureflag

A quick and dirty feature flag for Laravel

Downloads

304

Stars

0

Version

laravel-simple-featureflag

Latest Version on Packagist Software License Build Status Quality Score

This package gives you a quick and dirty way to use feature flags in Laravel 5+. It doesn't provide configuration by user, nor is it set up for A/B tests. It's a simple on/off switch.

Install

Install the package with Composer:

$ composer require JTGrimes/laravel-simple-featureflag

You'll need to update the providers array in config/app.php with the service provider for this package:

'providers' => [
    ...
    JTGrimes\FeatureFlag\ServiceProvider::class,
];

Finally, you'll need to publish the configuration file: You can publish the migration with:

$ php artisan vendor:publish --provider="JTGrimes\FeatureFlag\ServiceProvider"

Configuration

The default configuration file is shown below. The array keys are the names of the features that you're using feature flags for and the values are true/false based on whether the feature is enabled. (My convention is to set a FEATURE_* .env variable to 'on' or 'off', but any expression which is truthy will work.) Any feature which is not found in the config file will default to being enabled.

return [
    'something' => (env('FEATURE_SOMETHING', 'on') == 'on'),
    'something_else' => (env('SOME_OTHER_FEATURE', 'on') == 'on'),
    'one_more' => false,
];

Usage

The package provides a helper function. To determine whether a feature is enabled, you can call the feature() function from anywhere in your code.

   $permitAccess = feature('name');

I often find myself using this function in middleware to prevent access to pages which aren't available yet ...

        if (!feature('v2')) {
            if (str_contains($request->getUri(), 'v2')) {
                abort(Response::HTTP_NOT_FOUND);
            }
        }
        return $next($request);

There are also helpers for your Blade views:

@ifFeature ('test')
    Feature on
@else
    Feature off
@endif

@else is optional, but you must include @endif to close the if statement.

Change log

Please see CHANGELOG for more information on what has changed recently.

Testing

$ composer test

Contributing

Please see CONTRIBUTING and CONDUCT for details.

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

Credits

License

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

jtgrimes

Author

jtgrimes