PHPackages                             featurit/featurit-sdk-symfony - 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. featurit/featurit-sdk-symfony

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

featurit/featurit-sdk-symfony
=============================

Symfony SDK for FeaturIT

v0.8.0(1y ago)41.3kMITPHPPHP ^8.1

Since Mar 5Pushed 1y ago3 watchersCompare

[ Source](https://github.com/featurit/featurit-sdk-symfony)[ Packagist](https://packagist.org/packages/featurit/featurit-sdk-symfony)[ RSS](/packages/featurit-featurit-sdk-symfony/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (9)Versions (11)Used By (0)

FeaturIT SDK for Symfony
========================

[](#featurit-sdk-for-symfony)

Symfony wrapper of the PHP client for the FeaturIT Feature Flag management platform.

Description
-----------

[](#description)

This package aims to simplify the integration of the FeaturIT API in a Symfony project.

Getting started
---------------

[](#getting-started)

### Dependencies

[](#dependencies)

- PHP &gt;= 8.0.2
- symfony/framework-bundle &gt;= 5.2
- psr/http-client-implementation
- psr/simple-cache-implementation

### Installing

[](#installing)

`composer require featurit/featurit-sdk-symfony -W`

If there's no package providing psr/http-client-implementation, visit  and choose the package that better suits your project.

If there's no package providing psr/simple-cache-implementation, visit  and choose the package that better suits your project.

Inside your config/bundles.php file, add:

```
    Featurit\Client\Symfony\FeaturitBundle::class => ['all' => true],

```

If you want to create your own configuration file in order to customize things like the default FeaturitUserContextProvider, create a file in `config/packages/featurit.yaml`with the following contents:

```
featurit:
    tenant_identifier: '%env(string:FEATURIT_TENANT_IDENTIFIER)%'
    environment_key: '%env(string:FEATURIT_ENVIRONMENT_KEY)%'
    enable_analytics: '%env(bool:FEATURIT_ENABLE_ANALYTICS)%'
    enable_tracking: '%env(bool:FEATURIT_ENABLE_TRACKING)%'
    cache_ttl_minutes: '%env(int:FEATURIT_CACHE_TTL_MINUTES)%'
    send_analytics_interval_minutes: '%env(int:FEATURIT_SEND_ANALYTICS_INTERVAL_MINUTES)%'
    featurit_user_context_provider: '@my_service_implementing_featurit_user_context_provider'

```

You also need to create the proper environment variables in your `.env` file.

### Basic Usage

[](#basic-usage)

That's how you would use Featurit in one of your controllers, services, or anywhere inside your PHP codebase:

```
your_method(Featurit $featurit)
{
    if ($featurit->isActive('YOUR_FEATURE_NAME')) {
        your_feature_code();
    }
}

```

Or in order to check which is the version of your feature:

```
your_method(Featurit $featurit)
{
    if ($featurit->version('YOUR_FEATURE_NAME') == 'v1') {
        your_feature_code_for_v1();
    } else if ($featurit->version('YOUR_FEATURE_NAME') == 'v2') {
        your_feature_code_for_v2();
    }
}

```

### Twig extension

[](#twig-extension)

For convenience we provide 2 twig functions which allow to render html depending on the Feature Flag values.

Inside your twig template, you can use them like this:

```

    This code will always be visible

    {% if feature_is_active('MY_ACTIVE_FEATURE') %}
        Welcome to MY_ACTIVE_FEATURE!
    {% endif %}

    {% if feature_version_equals('FEATURE_WITH_VERSIONS', 'v1') %}
        Welcome to v1!
    {% elseif feature_version_equals('FEATURE_WITH_VERSIONS', 'v2') %}
        Welcome to v2!
    {% endif %}

```

### Defining your FeaturitUserContext

[](#defining-your-featuritusercontext)

In order to show different versions of a feature to different users, Featurit needs to know about the attributes your user has in a certain context.

You can define the context using the as follows:

```
your_method(Featurit $featurit)
{
    $contextData = get_your_user_context_data();

    $featurit->setUserContext(
        new DefaultFeaturitUserContext(
            $contextData['userId'],
            $contextData['sessionId'],
            $contextData['ipAddress'],
            [
                'role' => $contextData['role'],
                ...
            ]
        )
    );
}

```

### Defining a custom FeaturitUserContextProvider

[](#defining-a-custom-featuritusercontextprovider)

This is an alternative to using `$featurit->setUserContext(...);`.

By default, Featurit SDK for Symfony comes with a default FeaturitUserContextProvider adapted for Symfony, but if you want to create your own, create a service un your `services.yaml` file as follows:

```
services:
...
    Namespace\For\MyFeaturitUserContextProvider
        arguments:
            - arg1
            - arg2
            - ...

```

And then add it to the `featurit.yaml` config file as:

```
featurit:
    ...
    featurit_user_context_provider: '@Namespace\For\MyFeaturitUserContextProvider'

```

Let's say that your platform users have a "role" attribute that you use to decide which features you show to each user. In that case you could create an implementation like:

```
