- How do I install Laravel Pennant in a Laravel 11+ project?
- Run `composer require laravel/pennant` and execute the migration with `php artisan pennant:install`. This creates the `features` table and publishes the config file. No additional setup is needed for basic usage, but check `config/pennant.php` for driver and caching configurations.
- Can I use Pennant with Laravel 10 or older versions?
- Pennant is officially supported for Laravel 11–13. For Laravel 10, you may need to backport dependencies manually, but this isn’t officially tested. The package leverages Laravel 11+ features like enums and improved service container bindings, so compatibility isn’t guaranteed.
- How do I define and enable a feature flag in Pennant?
- Use the `Feature` facade: `Feature::create('new-feature', true)` to enable it globally. For scoped flags (e.g., per user), use `Feature::create('new-feature', true, ['user' => $userId])`. Flags are stored in the database and can be toggled via the `Feature` model or admin interface.
- Does Pennant support caching for better performance?
- Yes, Pennant supports caching via Redis or Memcached. Enable it in `config/pennant.php` by setting the `cache` option to `true`. Cache invalidation is automatic when flags are updated, but high-traffic apps may need to tune cache TTLs or use a dedicated cache driver for flags.
- How can I restrict feature access to specific users or roles?
- Use scopes to target flags. For example, `Feature::create('premium-feature', true, ['role' => 'admin'])` restricts access to users with the 'admin' role. Scopes can combine multiple conditions (e.g., `['user' => $id, 'tenant' => $tenantId]`), and Pennant evaluates them in order.
- Is there a way to gate routes or API endpoints with Pennant?
- Yes, use the `EnsureFeaturesAreActive` middleware. Add it to your `Kernel.php` or route group to block access if required flags aren’t enabled. For example: `Route::middleware(['auth', 'ensure-features-active:new-feature'])->group(...);`. This works for both web and API routes.
- Can I use Pennant with Vue.js or React frontend frameworks?
- For Blade templates, use the `@feature('flag-name')` directive to conditionally render content. For Vue/React, fetch flags via API endpoints (e.g., `/api/features`) or use JavaScript SDKs like `laravel-feature-flags` to sync flags dynamically. Pennant doesn’t include a frontend SDK, but the API is straightforward to consume.
- How do I test feature flags in my Laravel application?
- Mock the `Feature` facade in tests using Laravel’s testing helpers. For example: `Feature::shouldReceive('bool')->andReturn(true);`. For integration tests, use `Feature::create()` to set up test flags. Ensure your tests cover both enabled and disabled states to verify logic branches.
- What happens if I need to roll back a failed deployment with Pennant flags?
- Pennant flags are database-backed, so rolling back the database (e.g., via migrations or backups) will revert flag states. For manual overrides, use `Feature::set()` or the admin interface. If flags are cached, clear the cache (`php artisan cache:clear`) to sync changes across environments.
- Are there alternatives to Pennant for Laravel feature flags?
- Yes, alternatives include Spatie’s `laravel-feature-flags` (more flexible but heavier), `flagsmith-php`, or `launchdarkly-server-sdk`. Pennant stands out for its Laravel-native integration, minimal setup, and support for scopes and middleware. Choose Pennant if you want a lightweight, official Laravel solution without external dependencies.