Yes, you can use custom drivers with Laravel Pennant when integrated with Laravel Octane. Here is detailed information on how this works and what you need to consider:
Implementing Custom Drivers in Laravel Pennant
Laravel Pennant allows you to write your own custom storage drivers if the built-in ones do not meet your needs. Your custom driver must implement the `Laravel\Pennant\Contracts\Driver` interface, which requires defining several methods such as `define`, `get`, `set`, `delete`, and others to manage feature flags programmatically.
For example, a Redis-based custom driver might look like this:
php
namespace App\Extensions;
use Laravel\Pennant\Contracts\Driver;
class RedisFeatureDriver implements Driver
{
public function define(string $feature, callable $resolver): void {}
public function defined(): array {}
public function getAll(array $features): array {}
public function get(string $feature, mixed $scope): mixed {}
public function set(string $feature, mixed $scope, mixed $value): void {}
public function setForAllScopes(string $feature, mixed $value): void {}
public function delete(string $feature, mixed $scope): void {}
public function purge(array|null $features): void {}
}
You implement these methods to interact with your chosen storage backend (e.g., Redis, third-party feature flag services)[1][6].
Registering the Custom Driver
After implementing your driver, you register it with Pennant using the `Feature::extend` method inside a service provider's `boot` method:
php
use App\Extensions\RedisFeatureDriver;
use Laravel\Pennant\Feature;
use Illuminate\Contracts\Foundation\Application;
class AppServiceProvider extends ServiceProvider
{
public function boot(): void
{
Feature::extend('redis', function (Application $app) {
return new RedisFeatureDriver($app->make('redis'), $app->make('events'), []);
});
}
}
Then, you configure your custom driver in `config/pennant.php` under the `stores` array:
php
'stores' => [
'redis' => [
'driver' => 'redis',
'connection' => null,
],
// other stores...
],
You can then set `'redis'` as the default or use it selectively[1][6].
Using Custom Drivers with Laravel Octane
Laravel Octane is designed to supercharge Laravel applications by serving requests in a high-performance, long-running server environment. Pennant, being a Laravel package, integrates naturally with Laravel Octane.
However, when using custom drivers with Octane, you should be aware of Octane's persistent worker model. Since Octane keeps application state in memory between requests, any caching or stateful behavior in your custom Pennant driver must be safe for concurrency and long-lived processes. For example, if your driver uses in-memory caches or connections, ensure they are properly reset or managed to avoid stale data or memory leaks.
There is no official documentation explicitly stating incompatibility or special steps required for custom Pennant drivers with Octane. The general Laravel best practice applies: services and drivers should be stateless or properly reset between requests when running under Octane.
Additional Considerations
- If your custom driver wraps a third-party feature flag service (e.g., LaunchDarkly), you might also implement the `DefinesFeaturesExternally` interface to delegate feature definitions externally.
- Pennant supports scoping features to models or other identifiers, and you can customize how scopes are serialized for your driver by implementing the `FeatureScopeable` contract on your models. This ensures your driver receives the correct identifiers even under Octane's persistent environment.
- You can register multiple drivers and even use different drivers per feature if needed, though this requires additional management logic[2][3][6].
Summary
- Laravel Pennant supports custom drivers by implementing the `Driver` interface.
- Custom drivers are registered via `Feature::extend` in a service provider.
- Laravel Octane runs Laravel applications in a persistent server mode, so your custom driver must be safe for long-lived processes.
- No inherent conflict exists between Pennant custom drivers and Octane, but you must manage state carefully.
- You can integrate third-party feature flag services as custom drivers and leverage Pennant's flexibility fully under Octane.
This makes it entirely feasible and supported to use custom Laravel Pennant drivers within a Laravel Octane-powered application, provided you follow best practices for state management in Octane[1][6][10].
Citations:
[1] https://laravel.com/docs/12.x/pennant
[2] https://github.com/laravel/pennant/issues/54
[3] https://github.com/oneduo/laravel-pennant-launchdarkly
[4] https://wpwebinfotech.com/blog/laravel-pennant-guide/
[5] https://www.reddit.com/r/laravel/comments/10yto7q/laravel_pennant_simple_and_lightweight_feature/
[6] https://laravel.com/docs/11.x/pennant
[7] https://laracasts.com/discuss/channels/laravel/laravel-pennant-how-to-control-a-feature-for-all-users
[8] https://laracasts.com/episodes/2685
[9] https://bmehul.com/articles/laravel-pennant-a-guide-to-feature-flags
[10] https://www.youtube.com/watch?v=YGBvdAWt0W8