- Notifications
You must be signed in to change notification settings - Fork0
Laravel Feature Flags allows instant, zero-deployment toggling of application features.
License
codinglabsau/laravel-feature-flags
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Laravel Feature Flags allows instant, zero-deployment toggling of application features.
The state of each feature flag can be checked from anywhere in the application code (including via a@feature('name')
blade directive) to determine whether the conditions you set have been met to enable the feature.
Each feature can be in one of three states:
- On: enabled for everyone
- Off: disabled for everyone
- Dynamic: evaluated according to a feature-specific closure (with a fallback option)
composer require codinglabsau/laravel-feature-flags
php artisan vendor:publish --tag="feature-flags-migrations"php artisan migrate
php artisan vendor:publish --tag="feature-flags-config"
This package caches the state of features to reduce redundant database queries. The cache is expired whenever the feature state changes.
By default, this package will use the default cache configured in your application.
If you wish to change to a different cache driver, update your.env
:
FEATURES_CACHE_STORE=file
Create a new feature in the database and set the initial state:
useCodinglabs\FeatureFlags\Models\Feature;useCodinglabs\FeatureFlags\Enums\FeatureState;Feature::create(['name' =>'search-v2','state' => FeatureState::on()]);
Its recommended that you seed the features to your database before a new deployment or as soon as possible after a deployment.
Use the@feature
blade directive anywhere in your view files.
@feature('search-v2')// new search goes here@else// legacy search here@endfeature
Use theFeatureFlag
facade to conveniently check the state of a feature in your app code.
useCodinglabs\FeatureFlags\Facades\FeatureFlag;if (FeatureFlag::isOn('search-v2')) {// new feature code}else {// old code}
Registerfeature
as a route middleware in your HTTP Kernel to protect routes. A 404 response will be returned if the feature does not resolve to the on state.
// app/Http/Kernel.phpprotected$routeMiddleware = [// ...'feature' => \Codinglabs\FeatureFlags\Middleware\VerifyFeatureIsOn::class,];// routes/web.phpRoute::get('search-v2', \App\Http\Controllers\SearchV2Controller::class)->middleware('feature:search-v2');
@unlessfeature('search-v2')// no new features for you@endfeature
useCodinglabs\FeatureFlags\Facades\FeatureFlag;if (FeatureFlag::isOff('search-v2')) {// no new features for you}
If you want to know what the underlyingFeatureState
value is:
useCodinglabs\FeatureFlags\Facades\FeatureFlag;// value from Codinglabs\FeatureFlags\Enums\FeatureState$featureState = FeatureFlag::getState('search-v2');
To change the state of a feature you can call the following methods:
useCodinglabs\FeatureFlags\Facades\FeatureFlag;FeatureFlag::turnOn('search-v2');FeatureFlag::turnOff('search-v2');FeatureFlag::makeDynamic('search-v2');
Alternatively you can set the state directly by passing a feature state enum:
FeatureFlag::updateFeatureState('search-v2', FeatureState::on())
It is recommended that you only update a features state using the above methods as it will take care of flushing the cache and dispatching the feature updated event:
\Codinglabs\FeatureFlags\Events\FeatureUpdatedEvent::class
You should listen for theFeatureUpdatedEvent
event if you have any downstream implications when a feature state is updated, such as invalidating any cached items that are referenced in dynamic handlers.
A dynamic handler can be defined in theboot()
method of yourAppServiceProvider
:
useCodinglabs\FeatureFlags\Facades\FeatureFlag;FeatureFlag::registerDynamicHandler('search-v2',function ($feature,$request) {return$request->user() &&$request->user()->hasRole('Tester');});
Dynamic handlers will only be called when a feature is in thedynamic
state. This will allow you to define custom rules around whether that feature is enabled like in the example above where the user can only access the feature if they have a tester role.
Each handler is provided with the features name and current request as arguments and must return a boolean value.
You may also define a default handler which will be the catch-all handler for features that don't have an explicit handler defined for them:
FeatureFlag::registerDefaultDynamicHandler(function ($feature,$request) {return$request->user() &&$request->user()->hasRole('Tester');});
An explicit handler defined usingregisterDynamicHandler()
will take precedence over the default handler. If neither a default nor explicit handler has been defined then the feature will resolve tooff
by default.
Features must exist in the database otherwise aMissingFeatureException
will be thrown. This behaviour can be turned off by explicitly handling cases where a feature doesn't exist:
FeatureFlag::handleMissingFeaturesWith(function ($feature) {// log or report this somewhere...})
If a handler for missing features has been defined then an exception willnot be thrown and the feature will resolve tooff
.
To use your own model, update the config and replace the existing reference with your own model:
// app/config/feature-flags.php'feature_model' => \App\Models\Feature::class,
Make sure to also cast the state column to a feature state enum using theFeatureStateCast
:
// app/Models/Feature.phpuseCodinglabs\FeatureFlags\Casts\FeatureStateCast;protected$casts = ['state' => FeatureStateCast::class];
// app/Middleware/HandleInertiaRequest.phpuseCodinglabs\FeatureFlags\FeatureFlags;useCodinglabs\FeatureFlags\Models\Feature;Inertia::share(['features' =>function () {return Feature::all() ->filter(fn ($feature) => FeatureFlags::isOn($feature['name'])) ->pluck('name'); }]);
// app.jsVue.mixin({methods:{hasFeature:function(feature){returnthis.$page.features.includes(feature)}}})
<!-- SomeComponent.vue --><divv-if="hasFeature('search-v2')">Some cool new feature</div>
composertest
Please reviewour security policy on how to report security vulnerabilities.
The MIT License (MIT). Please seeLicense File for more information.
About
Laravel Feature Flags allows instant, zero-deployment toggling of application features.
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors2
Uh oh!
There was an error while loading.Please reload this page.