PHPackages                             flagpal/flagpal-for-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. flagpal/flagpal-for-laravel

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

flagpal/flagpal-for-laravel
===========================

An SDK for using the FlagPal in Laravel

v1.1.0(4mo ago)11.1k[5 PRs](https://github.com/FlagPal/flagpal-for-laravel/pulls)MITPHPPHP ^8.2CI passing

Since Sep 2Pushed 1mo ago2 watchersCompare

[ Source](https://github.com/FlagPal/flagpal-for-laravel)[ Packagist](https://packagist.org/packages/flagpal/flagpal-for-laravel)[ Docs](https://github.com/flagpal/flagpal-for-laravel)[ GitHub Sponsors]()[ RSS](/packages/flagpal-flagpal-for-laravel/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (6)Dependencies (21)Versions (12)Used By (0)

FlagPal for Laravel
===================

[](#flagpal-for-laravel)

[![Latest Version on Packagist](https://camo.githubusercontent.com/5e86ca25610e8fc82ff8c65642f87c8fa8b7957c7a4635893c3362417b0fe8a8/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f666c616770616c2f666c616770616c2d666f722d6c61726176656c2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/flagpal/flagpal-for-laravel)[![GitHub Tests Action Status](https://camo.githubusercontent.com/209bfcf09a8e92639e88f301233572a3a9d1079b40e991d0fe7a39943a518933/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f666c616770616c2f666c616770616c2d666f722d6c61726176656c2f72756e2d74657374732e796d6c3f6272616e63683d6d61696e266c6162656c3d7465737473267374796c653d666c61742d737175617265)](https://github.com/flagpal/flagpal-for-laravel/actions?query=workflow%3Arun-tests+branch%3Amain)[![GitHub Code Style Action Status](https://camo.githubusercontent.com/25ec14b6304b3044a59ada85a6988ef81b32c85b2f61aa696adf406d85bf0659/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f666c616770616c2f666c616770616c2d666f722d6c61726176656c2f6669782d7068702d636f64652d7374796c652d6973737565732e796d6c3f6272616e63683d6d61696e266c6162656c3d636f64652532307374796c65267374796c653d666c61742d737175617265)](https://github.com/flagpal/flagpal-for-laravel/actions?query=workflow%3A%22Fix+PHP+code+style+issues%22+branch%3Amain)[![Total Downloads](https://camo.githubusercontent.com/fddb749502463c951df70894d9930993a5df2845f272001d94a9b86bbd5aa8b0/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f666c616770616c2f666c616770616c2d666f722d6c61726176656c2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/flagpal/flagpal-for-laravel)

FlagPal is a Laravel package for resolving feature flags provided via flagpal.com API. It allows you to incrementally roll out new features, perform A/B testing, and manage feature access across your application with ease.

Features
--------

[](#features)

- Resolve feature flags from a remote API
- Support for multiple projects with different configurations
- Local resolution and built-in caching for improved performance
- Metric recording for feature usage
- Actor management for user-specific features
- Comprehensive logging

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require flagpal/flagpal-for-laravel
```

You can publish the config file with:

```
php artisan vendor:publish --tag="flagpal-for-laravel-config"
```

Configuration
-------------

[](#configuration)

After publishing the configuration file, you'll find it at `config/flagpal.php`. Here you can configure:

### API Connection

[](#api-connection)

```
// Base URL for the FlagPal API
'base_url' => env('FLAGPAL_URL'),

// Default project to use
'default_project' => env('FLAGPAL_PROJECT'),
```

### Projects Configuration

[](#projects-configuration)

```
'projects' => [
    'My Project' => [
        'name' => 'My Project',
        'bearer_token' => env('FLAGPAL_MY_PROJECT_TOKEN'),
    ],
    // Add more projects as needed
],
```

### Caching Options

[](#caching-options)

```
'cache' => [
    'driver' => 'default', // Use any cache driver from your cache.php config
    'ttl' => 60, // Cache TTL in seconds
],
```

### Logging Options

[](#logging-options)

```
'log' => [
    'driver' => 'default', // Use any log driver from your logging.php config
],
```

Usage
-----

[](#usage)

FlagPal is designed in a way to adapt to your application needs. You can use as many or as little features as you'd like. At the core, it consists of three basic concepts:

- Feature flags. They're the building blocks of your logic. For example, a feature flag called `new-api`.
- Experiments and Experiences (also known as Funnels). They target your currently provided feature flags and resolve new ones, by the rules defined in the FlagPal dashboard. The only difference between an Experiment or an Experience is that Experiments can have multiple variants, but an Experience only has one.
- Metrics. You may define the metrics you'd like to track for a specific Experiment to make business decisions. A Metric, for example, can be a `conversion`, `revenue`, `interaction`, or anything else. Be as abstract or specific as you need to be.

With this concept in mind, you can start resolving your features by chaining Experiments or Experiences one after another, and passing your features every time.

#### Pennant

[](#pennant)

As this is a Laravel package, it comes with a Laravel Pennant driver. FlagPal works completely with or without the driver. Examples will include usage for both cases. Keep in mind that FlagPal's goal is to give you **options**, while Pennant is rather opinionated. All of Pennant's and FlagPal's features coexist and may simply need a bit more verbose configuration. Since there is a lot of Laravel "magic" going behind the hood, please see this section on how to configure the Laravel Pennant driver.

### Basic Feature Resolution

[](#basic-feature-resolution)

#### With Pennant

[](#with-pennant)

```
use Laravel\Pennant\Feature;

// Assuming you have the driver set up as default in config/pennant.php
// Check if a specific feature is active. You define these features in FlagPal's dashboard
if (Feature::active('new-api')) {
    // Use the new API
} else {
    // Use the legacy API
}

// If the driver isn't default, you should call it every time: Feature::store('flagpal')->active('new-api')
```

#### Without Pennant

[](#without-pennant)

```
use FlagPal\FlagPal\FlagPal;

// Create a FlagPal instance or use dependency injection
$flagPal = app(FlagPal::class);

// Resolve features (returns an array of active features)
$features = $flagPal->resolveFeatures();

// Check if a specific feature is active. You define these features in FlagPal's dashboard
if (in_array('new-api', $features)) {
    // Use the new API
} else {
    // Use the legacy API
}
```

### Rich Feature Values

[](#rich-feature-values)

You can define your features not only in binary states (active/inactive), but store rich values as well. There are multiple value types available, that should cover most of your needs: boolean, string, integer, array, date

#### With Pennant

[](#with-pennant-1)

```
use Laravel\Pennant\Feature;

// Check a value of a specific feature
if (Feature::value('checkout-flow') === 'multi-step') {
    // Render a multi-step checkout flow
} else {
    // Render the checkout within a single page
}
```

#### Without Pennant

[](#without-pennant-1)

```
use FlagPal\FlagPal\FlagPal;

// Create a FlagPal instance or use dependency injection
$flagPal = app(FlagPal::class);

// Resolve features (returns an array of active features)
$features = $flagPal->resolveFeatures();

// Check a value of a specific feature
if ($features['checkout-flow'] === 'multi-step') {
    // Render a multi-step checkout flow
} else {
    // Render the checkout within a single page
}
```

### Resolving with pre-existing features

[](#resolving-with-pre-existing-features)

#### With Pennant

[](#with-pennant-2)

```
use Laravel\Pennant\Feature;

// Flags and values can be anything defined in your application
// and have the same names defined in FlagPal's dashboard

// These feature values can be retrieved from anywhere: your the database (like your User model), cache, other Pennant drivers. It's up to you
$currentFeatures = [
    'dark-mode' => true,
    'checkout-flow' => 'single-page',
    'trial-days-remaining' => 14,
];

$currentFeatures = new \FlagPal\FlagPal\Pennant\StatelessFeatures($currentFeatures);

// Check if a specific feature is active
if (Feature::for($currentFeatures)->active('show-trial-reminder')) {
    // Trigger some promotional message
}
```

#### Without Pennant

[](#without-pennant-2)

```
use FlagPal\FlagPal\FlagPal;

// Create a FlagPal instance or use dependency injection
$flagPal = app(FlagPal::class);

// Flags and values can be anything defined in your application
// and have the same names defined in FlagPal's dashboard

// These feature values can be retrieved from anywhere: your the database (like your User model), cache, other Pennant drivers. It's up to you
$currentFeatures = [
    'dark-mode' => true,
    'checkout-flow' => 'single-page',
    'trial-days-remaining' => 14,
];

$features = $flagPal->resolveFeatures($currentFeatures);

// Check if a specific feature is active
if (in_array('show-trial-reminder', $features)) {
    // Trigger some promotional message
}
```

### Working with Multiple FlagPal Projects

[](#working-with-multiple-flagpal-projects)

First, make sure you have your project configured in `config/flagpal.php`

#### With Pennant

[](#with-pennant-3)

To use multiple projects with Laravel Pennant, it's best to [register them as separate drivers](https://laravel.com/docs/master/pennant#registering-the-driver). You can skip registering them in your application's service provider, as this is already done by `FlagPalServiceProvider`. You only need to define your projects in `config/pennant.php`

```
