Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon What are the steps to integrate Laravel Pennant with Laravel Octane


What are the steps to integrate Laravel Pennant with Laravel Octane


To integrate Laravel Pennant with Laravel Octane, follow these detailed steps:

1. Install Laravel Pennant

- Use Composer to install Laravel Pennant in your Laravel 11.x project:


  composer require laravel/pennant
  

- Publish the Pennant configuration and migration files:

  php artisan vendor:publish --provider="Laravel\Pennant\PennantServiceProvider"
  

- Run the migrations to create the `features` table used by Pennant:

  php artisan migrate
  

2. Configure Laravel Pennant

- Open the `config/pennant.php` file.
- Define your feature flags in the `features` array, for example:

php
  return [
      'features' => [
          'new-dashboard' => false,
          'experimental-search' => true,
      ],
  ];
  

- Optionally, use environment variables to toggle features per environment:
php
  return [
      'features' => [
          'new-dashboard' => env('NEW_DASHBOARD_FEATURE', false),
          'experimental-search' => env('EXPERIMENTAL_SEARCH_FEATURE', true),
      ],
  ];
  

3. Define Feature Flags Programmatically

- In a service provider such as `AppServiceProvider`, define feature flags using the `Feature::define()` method with a callback to determine activation scope (e.g., user):

php
  use Laravel\Pennant\Feature;

  public function boot(): void
  {
      Feature::define('new-dashboard', function ($user) {
          return $user->isBetaTester(); // Activate for beta testers only
      });
  }
  

4. Install Laravel Octane

- Install Laravel Octane via Composer:


  composer require laravel/octane
  

- Run the Octane installation command and select your preferred server (e.g., Swoole or RoadRunner):

  php artisan octane:install
  

- Start the Octane server:

  php artisan octane:start
  

5. Ensure Pennant Compatibility with Octane

- Laravel Octane keeps the application in memory between requests, which can cause state to persist unexpectedly.
- Laravel Pennant uses feature flag resolution that may cache feature states during a request lifecycle.
- To avoid stale feature flag states when using Octane, clear or reset Pennant's feature cache or resolved flags at the start of each request.
- This can be done by adding middleware or hooking into Octane's request lifecycle to reset Pennant's state, for example:

php
  use Laravel\Pennant\Feature;

  public function handle($request, Closure $next)
  {
      Feature::flush(); // Clear resolved feature flags cache

      return $next($request);
  }
  

- Register this middleware globally or in Octane's request lifecycle to ensure feature flags are fresh per request.

6. Use Laravel Pennant in Your Application

- In controllers or views, check feature flags scoped to the user or other context:

php
  use Laravel\Pennant\Feature;

  if (Feature::for(auth()->user())->active('new-dashboard')) {
      // Show new dashboard
  } else {
      // Show old dashboard
  }
  

- Use Blade directives for convenience:
blade
  @feature('new-dashboard')
      @include('dashboard.new')
  @elsefeature
      @include('dashboard.old')
  @endfeature
  

Summary

- Install and configure Laravel Pennant normally.
- Install Laravel Octane and run your app with it.
- Because Octane keeps the app in memory, explicitly clear Pennant's cached feature flags each request to prevent stale flag states.
- Use Pennant’s API and Blade directives as usual to control feature availability.

This approach ensures Laravel Pennant's feature flags work correctly and consistently in the persistent environment provided by Laravel Octane[1][2][6][7][8].

Citations:
[1] https://laravel.com/docs/11.x/pennant
[2] https://laravel.com/docs/11.x/octane
[3] https://www.honeybadger.io/blog/a-guide-to-feature-flags-in-laravel/
[4] https://redberry.international/laravel-pennant-feature-flagging-made-easy/
[5] https://bmehul.com/articles/laravel-pennant-a-guide-to-feature-flags
[6] https://wpwebinfotech.com/blog/laravel-pennant-guide/
[7] https://dev.to/aleson-franca/laravel-pennant-releasing-features-with-feature-flags-3lm1
[8] https://www.youtube.com/watch?v=YGBvdAWt0W8