PHPackages                             check24/feature-flag-bundle - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. check24/feature-flag-bundle

ActiveSymfony-bundle[Utility &amp; Helpers](/categories/utility)

check24/feature-flag-bundle
===========================

Toggleable feature flags for your symfony application with various sources like .env, cookie, etc.

1.2.0(4y ago)01.3k3[1 PRs](https://github.com/byWulf/feature-flag-bundle/pulls)MITPHPPHP ^7.2|^8.0

Since Oct 9Pushed 4y agoCompare

[ Source](https://github.com/byWulf/feature-flag-bundle)[ Packagist](https://packagist.org/packages/check24/feature-flag-bundle)[ RSS](/packages/check24-feature-flag-bundle/feed)WikiDiscussions master Synced 3d ago

READMEChangelog (7)Dependencies (8)Versions (12)Used By (0)

FeatureFlagBundle
=================

[](#featureflagbundle)

Easily toggle features in your Symfony app. Built-in providers are .env, cookies and userAgent. But you can extend the system with own providers (f.e. for A/B testing).

Install
-------

[](#install)

Install the package via composer:

```
composer require check24/feature-flag-bundle

```

Usage
-----

[](#usage)

### Accessing feature flag states

[](#accessing-feature-flag-states)

#### Limit access to controller actions

[](#limit-access-to-controller-actions)

You can use the `IsActive` annotation to let the action only be accessible if the given feature is active. Otherwise a 403 is thrown:

```
use Shopping\FeatureFlagBundle\Annotation\IsActive;

/**
 * @Route("test")
 * @IsActive("foobar")
 * @return Response
 */
public function test(): Response
{
    return new Response('I can see you.');
}
```

#### As a dependency injection

[](#as-a-dependency-injection)

To access the state of a feature inside a service, you can inject the `FeatureFlagInterface`

```
use Shopping\FeatureFlagBundle\Service\FeatureFlagInterface;

class SomeService
{
    private $featureFlag;

    public function __construct(FeatureFlagInterface $featureFlag)
    {
        $this->featureFlag = $featureFlag;
    }

    public function getNumber(): int
    {
        if ($this->featureFlag->isActive('foobar')) {
            return 2;
        }

        return 1;
    }
}
```

#### In Twig

[](#in-twig)

Use the twig function `is_active` to check if a feature is enabled:

```
{% if is_active('foobar') %}
    Awesome!
{% else %}
    Default...
{% endif %}
```

### Configuring feature flag providers

[](#configuring-feature-flag-providers)

In your `config/packages/shopping_feature_flag.yaml` configuration, you can configure the built-in providers to define, which feature flag (key) should be active, when the user inputs on of the values:

```
shopping_feature_flag:
    providers:
        cookie:
            values:
                test1: 1234
                test2: [5678,9999]
        userAgent:
            values:
                test: foobar/chrome
                test2: [foobar/chrome, foobar/firefox]
```

You can enable/disable some of the built in providers.

### Toggling features

[](#toggling-features)

Feature flags can be toggled by the configured providers. If at least one of the providers reports the feature as active, it is active.

#### .env

[](#env)

Toggle a feature environment wide in your `.env` file. You could f.e. disable the feature in your productive `.env` file but enable it in your staging `.env` file.

```
FEATUREFLAG_FOOBAR=1
```

(Prefix "FEATUREFLAG\_" and uppercase feature flag name)

#### Cookie

[](#cookie)

Activate a feature only for yourself by setting a cookie in your browser, f.e. in Chrome with F12 -&gt; Application -&gt; Cookies

```
featureFlag_foobar=1

```

(Prefix "featureFlag\_" and feature flag name)

If you haven't specified a specific value in the config for the feature flag, any value will activate the feature. Use the config to make the features more secure.

#### User-Agent

[](#user-agent)

Activate a feature only for yourself by setting a custom User-Agent in your browser (f.e. with the Chrome plugin "User-Agent Switcher").

Configure in the `shopping_feature_flag.yaml`, what parts of the User-Agent activates what feature:

```
shopping_feature_flag:
    providers:
        userAgent:
            values:
                foobar: foobar/chrome
```

-&gt; When using a User-Agent containing the string `foobar/chrome`, the Feature "foobar" is active. This way it is possible to activate multiple features with one User-Agent, which contains all needed keys for the features while preserving the browser/device detection of the website.

### Create own provider

[](#create-own-provider)

If you want to activate features in a custom way, you want to write your own provider.

In this example we create a time-based provider, so the specific feature "morningShow" is only activated between 8 and 10 a.m. .

First create a provider class, implement the `FeatureFlagInterface` with the `isActive()` method. This method should return true if the given feature is active. Otherwise return false.

```
//src/FeatureFlagProvider/MorningProvider.php

namespace App\FeatureFlagProvider;

class MorningProvider implements FeatureFlagInterface
{
    /**
     * @param string $featureFlag
     *
     * @return bool
     */
    public function isActive(string $featureFlag): bool
    {
        if ($featureFlag !== 'morningShow') {
            return false;
        }

        $hour = (new \DateTime())->format('G');

        return $hour >= 8 && $hour
