- How do I install Laravel Pennant in my Laravel project?
- Run `composer require laravel/pennant` to install the package. Then publish the migration and configuration files with `php artisan pennant:install`, and run the migrations using `php artisan migrate`. The package is ready to use after these steps.
- What Laravel versions does Pennant support?
- Pennant officially supports Laravel 11 and 12. Laravel 10 is supported with backported patches, but older versions (e.g., 9.x) are not recommended. Always check the [Laravel documentation](https://laravel.com/docs/pennant) for the latest compatibility details.
- Can I use Pennant for A/B testing in my Laravel app?
- Yes, Pennant supports A/B testing by defining flags with different variations (e.g., `feature_x_variant_a` and `feature_x_variant_b`). You can scope flags to users or sessions and evaluate them dynamically in your Blade templates or middleware.
- How do I integrate Pennant with Laravel Blade templates?
- Use the `@feature` and `@featureany` Blade directives to conditionally render content. For example, `@feature('new-dashboard')` will only render the enclosed content if the flag is enabled. This works seamlessly with Livewire and Inertia.js.
- Does Pennant support Redis or other cache backends for flags?
- Yes, Pennant includes a cache driver that works with Redis, Memcached, or Laravel’s default cache. Configure the driver in `config/pennant.php` and set `driver` to `cache`. Flags will automatically sync with your cache backend.
- How do I restrict access to routes based on feature flags?
- Use the `EnsureFeaturesAreActive` middleware to gate routes. Add it to your `routes/web.php` or `app/Http/Kernel.php` under the `$routeMiddleware` array. For example, `Route::middleware(['web', 'features:new-dashboard'])->group(...);`.
- Can I test feature flags in my Laravel tests (Pest/PHPUnit)?
- Yes, Pennant provides a fake helper for testing. Use `Pennant::fake()` to simulate flag states in your tests. For example, `Pennant::fake(['new-dashboard' => true])` will override the flag during testing without affecting production.
- What’s the best way to manage feature flags in production?
- For production, use the database driver (default) for persistence and the cache driver for performance. Enable `cache_flush` in the driver config to ensure flags update in real-time. Avoid frequent flag changes during high-traffic periods to minimize cache invalidation.
- How do I migrate from another feature flag package (e.g., Spatie Feature Flags) to Pennant?
- Export your existing flags from the old package (e.g., as JSON or database dumps) and import them into Pennant’s database table. Use `Pennant::create()` or bulk operations like `Pennant::setMany()` to populate flags. Test thoroughly in staging before switching in production.
- Does Pennant support multi-region or distributed deployments?
- For multi-region setups, use a shared cache like Redis or DynamoDB as your flag storage. Configure the cache driver in `config/pennant.php` and ensure all regions point to the same cache backend. This ensures consistent flag evaluation across deployments.