Skip to content
A Laravel package for handling feature flags
PHP HTML
Branch: master
Clone or download
Latest commit ad5135e Jul 11, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
config Initial Commit Jul 15, 2019
migrations Initial Commit Jul 15, 2019
src Initial Commit Jul 15, 2019
tests Initial Commit Jul 15, 2019
.editorconfig Initial Commit Jul 15, 2019
.gitattributes Initial Commit Jul 15, 2019
.gitignore Initial Commit Jul 15, 2019
.scrutinizer.yml Initial Commit Jul 15, 2019
.styleci.yml Initial Commit Jul 15, 2019
.travis.yml Initial Commit Jul 15, 2019
CHANGELOG.md Initial Commit Jul 15, 2019
CONTRIBUTING.md Initial Commit Jul 15, 2019
LICENSE.md Initial Commit Jul 15, 2019
README.md Initial Commit Jul 15, 2019
composer.json Initial Commit Jul 15, 2019
phpunit.xml.dist Initial Commit Jul 15, 2019

README.md

Feature Flags for Laravel

Latest Version on Packagist Build Status Quality Score Code Coverage Total Downloads StyleCI

A Feature flag is sometimes also referred to as a feature toggle or feature switch. Ultimately it's a coding strategy to be used along with source control to make it easier to continuous integrate and continuous deployment. The idea of the flags works by essentially safe guarding sections of code from executing if a feature flag isn't in a switched on state.

This package aims to make implementing such flags across your application a great deal easier by providing solutions that work with not only your code but your routes, blade files, task scheduling and validations.

Installation

You can install the package via composer:

composer require ylsideas/feature-flags

Once installed you should publish the config with the following command.

php artisan vendor:publish --provider=YlsIdeas\\FeatureFlags\\FeatureFlagsServiceProvider --tag=config

You can customise the features.php config in a number of ways. By default four storage drivers for the feature flags are provided, config, database, redis and chain. the first three are pretty straight forward but the chain is essentially a composite that allows you to store across all three. For example you might want to query a feature that's hardcoded in the config. If it does not exist it will then go on to check redis. If it's not stored there, then it'll check the database. Afterwards it can update the other sources to improve flag checking times.

To use the Database driver you will need to add the migration. You can do this by using the publish command.

php artisan vendor:publish --tag=features-migration

Everything is enabled by default but if you want to turn off several features add the following method calls to the boot method of app/Providers/AppServiceProvider.php in your project.

Features::noBlade();
Features::noScheduling();
Features::noValidations();
Features::noCommands();

To install the middleware you'll have to add it to your $routeMiddleware inside app/Http/Kernel.php file.

protected $routeMiddleware = [
    'feature' => \YlsIdeas\FeatureFlags\Middleware\FeatureFlagState::class,
];

Usage

Checking feature accessibility

You can use the accessible method to check if a feature is on or off.

Features::accessible('my-feature') // returns true or false

Blade Views

the @feature blade directive is a simple @if shortcut to hide or display certain parts of the view depending on the state of the feature. A second argument flips the state e.g. it will display the contents of the if statement if the feature is off.

@feature('my-feature')
    <p>Your feature flag is turned on.
@endfeature

@feature('my-feature', false)
    <p>Your feature flag is turned off.
@endfeature

Routing Middleware

The middleware will cause routes to be blocked if the specified feature does not have the correct state.

Route::get('/', 'SomeController@get')->middleware('feature:my-feature')
Route::get('/', 'SomeController@get')->middleware('feature:my-feature,on')
Route::get('/', 'SomeController@get')->middleware('feature:my-feature,off,404')

Validation Rules

Fields can be marked as required depending on if the feature is in a particular state.

Validator::make([
    'name' => 'Peter'
    'place' => 'England',
    'email' => 'peter.fox@ylsideas.co'
], [
    'name' => 'requiredWithFeature:my-feature' // required
    'place' => 'requiredWithFeature:my-feature,on' // required
    'email => 'requiredWithFeature:my-feature,off' // not required
]);

Task Scheduling

Using the following will determine if a task will run on schedule depending on the state of the feature.

$schedule->command('emails:send Peter --force')
    ->skipWithFeature('my-feature')
    
$schedule->command('emails:send Peter --force')
    ->skipWithoutFeature('my-other-feature')    

Artisan Commands

You may run the following commands to toggle the on or off state of the feature.

php artisan feature:on my-feature

php artisan feature:off my-feature

Testing

composer test

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email peter.fox@ylsideas.co instead of using the issue tracker.

Credits

License

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

Laravel Package Boilerplate

This package was generated using the Laravel Package Boilerplate.

You can’t perform that action at this time.