mobihouse/laravel-pennant-cookie

A cookie driver for Laravel Pennant

1.0.0 2024-08-12 08:18 UTC

This package is not auto-updated.

Last update: 2024-10-21 09:11:57 UTC


README

Packagist License Packagist Version

Laravel Pennant - Cookie Driver

This package adds the ability to remember feature flags/values from Laravel pennant in a cookie, which allows you to have persisted values for anonymous users.

Important

This driver is most suited for tracking anonymous users, if you want to track actual users it will be more beneficial to use the database driver.

Usage

Install the package using:

composer require mobihouse/laravel-pennant-cookie

And configure it by adding a new store to the config/pennant.php config file:

return [

    'stores' => [
        
        // ...
        'cookie' => [
            'driver' => 'cookie'
        ]

    ]

]

Now that the store is configured set your PENNANT_STORE environment variable to cookie (or set the default key in the pennant config to cookie).

Scope

Laravel Pennant will try to use the auth scope by default and thus look for a current user. Though the driver will work with this it isn't it's intended purpose, so the easiest way to use anonymised scope is setting the default scope to null:

<?php
 
namespace App\Providers;
 
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\ServiceProvider;
use Laravel\Pennant\Feature;
 
class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     */
    public function boot(): void
    {
        Feature::resolveScopeUsing(fn () => null);
 
        // ...
    }
}

Options

By default the cookie that gets set will live for a year, if you want to change the default lifetime you can do the following in the config:

return [

    'stores' => [
        'cookie' => [
            'driver' => 'cookie',
            'lifetime' => 3600, // Live for one hour
        ]
    ]

]

Tests

Running the automated tests can be done by

./vendor/bin/pest