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

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

featurit/featurit-sdk-laravel
=============================

Laravel SDK for FeaturIT

v0.12(2mo ago)3112↓50%MITPHPPHP ^8.0.2

Since Dec 6Pushed 2mo ago1 watchersCompare

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

READMEChangelog (10)Dependencies (6)Versions (18)Used By (0)

FeaturIT SDK for Laravel
========================

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

Laravel 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 Laravel project.

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

[](#getting-started)

### Dependencies

[](#dependencies)

- PHP &gt;= 8.0.2
- laravel/framework &gt;= 5.1
- psr/http-client-implementation
- psr/simple-cache-implementation

### Installing

[](#installing)

`composer require featurit/featurit-sdk-laravel`

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/app.php file, in the providers array add:

```
        /*
         * Package Service Providers...
         */

        Featurit\Client\Laravel\FeaturitServiceProvider::class,

```

If you want to publish the default configuration file in order to customize things like the default FeaturitUserContextProvider, use the following command:

`php artisan vendor:publish --provider="Featurit\Client\Laravel\FeaturitServiceProvider"`

### Basic Usage

[](#basic-usage)

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

```
if (Featurit::isActive('YOUR_FEATURE_NAME')) {
    your_feature_code();
}

```

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

```
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();
}

```

### Blade directives

[](#blade-directives)

For convenience we provide 3 blade directives which allow to load blade components depending on the Feature Flag values.

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

```

    This code will always be visible

    @ifFeatureIsActive('MY_ACTIVE_FEATURE')
        This will be visible
    @endifFeatureIsActive

    @ifFeatureIsNotActive('MY_ACTIVE_FEATURE')
        This will NOT be visible
    @endifFeatureIsNotActive

    @ifFeatureVersionEquals('FEATURE_WITH_VERSIONS', 'v1')
        Welcome to v1!
    @endifFeatureVersionEquals

    @ifFeatureVersionEquals('FEATURE_WITH_VERSIONS', 'v2')
        Welcome to v2!
    @endifFeatureVersionEquals

```

### 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:

```
$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 Laravel comes with a default FeaturitUserContextProvider in your config/featurit.php file

```
'featurit_user_context_provider' => Featurit\Client\Laravel\Providers\LaravelFeaturitUserContextProvider::class,

```

But you can create your own implementation in order to add custom attributes so they can be used in the segmentation process.

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:

```
