jtgrimes/laravel-simple-featureflag

A quick and dirty feature flag for Laravel

dev-master / 1.0.x-dev 2017-06-16 04:05 UTC

This package is not auto-updated.

Last update: 2024-12-21 21:42:17 UTC


README

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 jtgrimes@gmail.com instead of using the issue tracker.

Credits

License

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