Download the PHP package friendsofcat/laravel-feature-flag without Composer

On this page you can find all versions of the php package friendsofcat/laravel-feature-flag. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package laravel-feature-flag

Feature Flags In Laravel

CI-CD

Overview

You can find a comprehensive blog post about this library here. This project is a work in progress.

We are working on using FeatureFlags or Toggles in our applications. For one we are aiming to do all our work on mainline branch at all times so this would be a key coding discipline to use FeatureFlags so we can hide a feature in progress knowing it will not interfere with the application. For example if a hotfix or another feature is ready to go to production we can push that with no worries of the in progress feature.

At the core we use this library Atriedes/feature as it has the logic needed to consider common feature flag states eg user, users, on, off, groups, admin, internal, random etc. However, we are also mixing in some nice Laravel Authorization features so you can do things like:

In a blade template:

@can('feature-flag', 'add-twitter-field')
<!-- code here -->
@endcan

Or in PHP:

if (Gate::allows('feature-flag', 'awesome-feature')) {
    <!-- code here -->
}
if (Gate::denies('feature-flag', 'awesome-feature')) {
    <!-- code here -->
}

If you need to pass your feature flags to a front-end JS framework like Angular or Vue.js, you can do so by using the FeatureFlagsForJavascript::get() static method.

This uses this library https://github.com/laracasts/PHP-Vars-To-Js-Transformer to put this info into the windows object, and for Angular the $window now you can access it:

JavaScript::Put(
            [
                'pusher_public_key' => env('PUSHER_PUBLIC'),
                'feature_flags'     => FeatureFlagsForJavascript::get()
            ]
        );

Installing

Require the package using composer:

composer require "friendsofcat/laravel-feature-flag"

Add the following to your config/app.php providers array:

FriendsOfCat\LaravelFeatureFlags\FeatureFlagsProvider::class,

Publish the package migrations:

php artisan vendor:publish --provider="FriendsOfCat\LaravelFeatureFlags\FeatureFlagsProvider" --tag='migrations'

Then run migration to setup the base table:

php artisan migrate

This package creates a number of routes. They can be overridden by publishing the views:

php artisan vendor:publish --provider="FriendsOfCat\LaravelFeatureFlags\FeatureFlagsProvider" --tag='views'

This will then place the files in resources/vendors/laravel-feature-flags. Just note that the views @extends('layouts.default') so if yours differs you will need to make an adjustment to the published views files.

Next, publish the configuration:

php artisan vendor:publish --provider="FriendsOfCat\LaravelFeatureFlags\FeatureFlagsProvider" --tag='config'

Important: The routes detault to being projected by the 'auth' middleware but you should check your installation to make sure permissions are acceptable. Middleware settings are configurable in 'config/laravel-feature-flag.php' file.

Make sure to set the default_view as well for the layout.

config/laravel-feature-flag.php

Your .env

Usage

Visit /admin/feature_flags to manage features via the UI.

Checking if a feature flag exists

For this you can use the exists() method

if(\FriendsOfCat\LaravelFeatureFlags\Feature::exists('see-twitter-field'))
{
  //do something
}

Enable for User Roles

You can enable a feature flag for specific user roles, by using the roles variant in the configuration form

i.e.

{ "roles": ["admin", "dev"]}

If you don't have a roles property in your User model, you just need to implement the FeatureFlagsEnabler Interface and use FeatureFlagUserRoleTrait

use FriendsOfCat\LaravelFeatureFlags\FeatureFlagsEnabler;
use FriendsOfCat\LaravelFeatureFlags\FeatureFlagUserRoleTrait;

class User extends Authenticatable implements FeatureFlagsUserRoles
{
    use AuthenticableTrait, FeatureFlagUserRoleTrait;
}

Enable for User Teams

You can enable a feature flag for specific user teams, by using the teams variant in the configuration form

i.e.

{ "teams": ["Team 1", "Team 2"]}

If you don't have a teams property in your User model, you just need to implement the FeatureFlagsEnabler Interface and use FeatureFlagUserRoleTrait

use FriendsOfCat\LaravelFeatureFlags\FeatureFlagsEnabler;
use FriendsOfCat\LaravelFeatureFlags\FeatureFlagUserRoleTrait;

class User extends Authenticatable implements FeatureFlagsUserRoles
{
    use AuthenticableTrait, FeatureFlagUserRoleTrait;
}

Usage Non Auth

Sometimes you are not using this at the Auth user level, it is rare for most of our use cases but for non authenticated situations you can just use this

if(\FriendsOfCat\LaravelFeatureFlags\Feature::isEnabled('see-twitter-field'))
{
  //do something
}

Remember you needed to put this into the database, so it is on or off. You might not have a UI, maybe this is a microservice for example, so just migrate the state into the database for example

$feature = new FeatureFlag();
$feature->key = "see-twitter-field";
$feature->variants = "on"; //or "off"
$feature->save();

Now when the FeatureFlag Provider instantiates it will set this as the "World" state and you can access it via the isEnabled "on" being true and "off" being false.

Syncing Flags

Feature flags can be synchronised using the provided feature-flag:sync command. This will sync flags defined in the sync_flags configuration in the laravel-feature-flag.php config file. The format for this flag configuration is "key => default value". By default, any flags that are removed will be removed from storage. There is a --skip-cleanup flag available to skip this step.

Demo / Example

If you want to try the demo/example also include the following in your config/app.php providers array:

FriendsOfCat\LaravelFeatureFlags\ExampleFeatureProvider::class

and then run:

php artisan vendor:publish --provider="FriendsOfCat\LaravelFeatureFlags\ExampleFeatureProvider" --tag='migrations'
php artisan migrate

It has a rollback to help clean up after.

There is a dummy route called /admin/feature_flags/example that you can visit and it will show that it is not on. But if you then go to the admin UI /admin/feature_flags you can toggle it on and off.

Testing

Helper Package

There is the settings page which I do have some Laravel tests for that you can run once the package is installed.

Also if you are trying to test the use of it in your work you can use the helper trait in your test class

Then from there factory out your additions and state then reregister the world

TODO


All versions of laravel-feature-flag with dependencies

PHP Build Version
Package Version
Requires php Version ^7.3|^8.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package friendsofcat/laravel-feature-flag contains the following files

Loading the files please wait ....