PHP code example of featurit / featurit-sdk-laravel

1. Go to this page and download the library: Download featurit/featurit-sdk-laravel library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

featurit / featurit-sdk-laravel example snippets


        /*
         * Package Service Providers...
         */

        Featurit\Client\Laravel\FeaturitServiceProvider::class,

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



namespace My\Namespace\Of\Choice;

use Featurit\Client\Modules\Segmentation\DefaultFeaturitUserContext;
use Featurit\Client\Modules\Segmentation\FeaturitUserContext;
use Featurit\Client\Modules\Segmentation\FeaturitUserContextProvider;
use Illuminate\Support\Facades\Auth;

class MyCustomFeaturitUserContextProvider implements FeaturitUserContextProvider
{
    public function getUserContext(): FeaturitUserContext
    {
        if (! Auth::check()) {
            return new DefaultFeaturitUserContext(
                null,
                null,
                null,
                [
                    'role' => 'Guest',
                ]
            );
        }

        $user = Auth::user();

        $userId = $user->getAuthIdentifier();
        $sessionId = session()->getId();
        $ipAddress = request()->ip();
        
        $role = $user->role;

        return new DefaultFeaturitUserContext(
            $userId,
            $sessionId,
            $ipAddress,
            [
                'role' => $role,
            ]
        );
    }
}

'featurit_user_context_provider' => My\Namespace\Of\Choice\MyCustomFeaturitUserContextProvider::class,