Backend Development 4 min read

Using Laravel Pennant for Feature Flags in Laravel 10

Laravel Pennant, introduced with Laravel 10, provides a robust feature‑flag system that lets developers gradually roll out new functionality, perform A/B testing, and implement trunk‑based development strategies, with examples showing both closure‑based and class‑based definitions, controller usage, and middleware integration.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Using Laravel Pennant for Feature Flags in Laravel 10

Laravel Pennant is a package created by the Laravel team and shipped with Laravel 10, offering a powerful feature‑flag system for your applications.

Feature flags enable you to confidently roll out new features incrementally, conduct A/B tests on interface designs, and support trunk‑based development workflows.

The package is officially maintained, well‑tested, and provides several convenient ways to define flags. You can define a flag directly in AppServiceProvider :

<code>public function boot(): void {
    Feature::define('beta-testers', fn (User $user) => match (true) {
        $user->isBetaTester() => true,
        default => false,
    });
}</code>

Alternatively, you can use a class‑based resolver:

<code>class BetaTesters {
    public function resolve(User $user): mixed {
        return match (true) {
            $user->isBetaTester() => true,
            default => false,
        };
    }
}</code>

In a controller you can conditionally execute code based on a flag using Feature::when :

<code>class PodcastController {
    public function index(Request $request): Response {
        return Feature::when(NewApi::class,
            fn () => $this->resolveNewApiResponse($request),
            fn () => $this->resolveLegacyApiResponse($request)
        );
    }
}</code>

You can also inject dependencies and switch between API versions based on a flag:

<code>class PodcastController {
    public function __construct(private readonly RedirectAction $action) {}
    public function index(Request $request): Response {
        return Feature::when(BetaTester::class,
            fn () => $this->action->handle('v2'),
            fn () => $this->action->handle('v1')
        );
    }
}</code>

A middleware example demonstrates redirecting requests when a flag is active:

<code>class VersionMiddleware {
    public function handle(Request $request, Closure $next): mixed {
        if (Feature::active('beta-tester')) {
            return new RedirectResponse(uri: 'generate the url here');
        }
        return $next($request);
    }
}</code>

These examples illustrate that the possibilities with Laravel Pennant are limited only by imagination, allowing you to enhance your application with clean, flexible feature‑flag logic.

backendPHPFeature FlagsLaravelLaravel10Pennant
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.