Laravel Flagger
Flagger is a package that has been designed to help you on enabling feature flags in Laravel projects., (*1)
Version Compatibility
| Laravel |
Flagger |
| 5.3.x |
1.x.x |
| 5.4.x |
2.x.x |
Installation
To install through composer, simply add the following in your composer.json file:, (*2)
{
"require": {
"leettech/laravel-flagger": "~2.0"
}
}
And then run composer install., (*3)
Quick Installation
The above installation can also be simplified by using the following command:, (*4)
composer require "leettech/laravel-flagger=~2.0"
Configuration
After installing the Flagger package, register the FlaggerServiceProvider in your config/app.php configuration file:, (*5)
'providers' => [
// Other service providers...
Leet\Providers\FlaggerServiceProvider::class,
],
Also, add the Flagger facade to the aliases array in your app configuration file:, (*6)
'aliases' => [
// Other aliases...
'Flagger' => Leet\Facades\Flagger::class,
],
Then run the migration script to create features and flaggables tables:, (*7)
php artisan migrate
Publish the package configuration:, (*8)
php artisan vendor:publish --provider="Leet\Providers\FlaggerServiceProvider"
And, in your config/flagger.php configuration file, specify which model will have feature flags associated to it (by default it's set to App\User::class)., (*9)
Usage
First of all, make sure you have inserted your features in the features table in the database. You can use the model Leet\Models\Feature for this:, (*10)
\Leet\Models\Feature::create([
'name' => 'notifications',
'description' => 'Notifications feature'
]);
flag
Use \Flagger::flag($flaggable, $feature) to attach a feature to a model:, (*11)
$user = \App\User::first();
\Flagger::flag($user, 'notifications');
You can also add Leet\Models\FlaggerTrait to the model in order to make flagger methods available from it:, (*12)
class User extends Model
{
use \Leet\Models\FlaggerTrait;
}
$user = \App\User::first();
$user->flag('notifications');
flagMany
Use \Flagger::flagMany($flaggables, $feature) to attach a feature to a collection of models:, (*13)
$users = \App\User::all();
\Flagger::flagMany($users, 'notifications');
hasFeatureEnabled
Anywhere in the application, you can check if a user has access to a feature:, (*14)
if ($user->hasFeatureEnabled('notifications')) {
doSomething();
}
FlaggerMiddleware
To use the FlaggerMiddleware, you have to declare it in the application kernel:, (*15)
protected $routeMiddleware = [
// Other middleware...
'flagger' => \Leet\Middleware\FlaggerMiddleware::class,
];
And on any authenticated route:, (*16)
Route::get('notifications', 'NotificationsController@index')->middleware('flagger:notifications');
or, (*17)
Route::group(['middleware' => 'flagger:notifications'], function () {
Route::get('notifications', 'NotificationsController@index');
Route::post('notifications', 'NotificationsController@store')
});
Getting enabled features for a model
By adding Leet\Models\FlaggerTrait to your model, you are able to access its enabled features:, (*18)
// returns the features a user have access to
$user->features;
Flagger command
The flagger command accepts an integer, array, or a path to a csv containing a list of integers and adds a flag to each of them:, (*19)
php artisan flagger notifications 1
// OR
php artisan flagger notifications 1 2 3
// OR
php artisan flagger notifications users.csv
// OR
php artisan flagger notifications users.csv --chunk=100
Be sure to create the flag before attempting to add it to any entity., (*20)