Backend Development 4 min read

Using Laravel Pennant for Feature Flags in Laravel 10

This article introduces Laravel Pennant, the official feature‑flag package for Laravel 10, explains its benefits for gradual releases and A/B testing, and provides clean code examples showing how to define and use feature flags within service providers, controllers, and middleware.

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

Laravel Pennant is an official package created by the Laravel team and shipped with Laravel 10 to provide feature flags for your application.

Feature flags let you confidently roll out new features gradually, A/B test new UI designs, and complement trunk‑based development strategies.

The package is the latest member of the official Laravel ecosystem, offering a well‑tested, robust set of capabilities.

You can simply use your AppServiceProvider to create new features.

Example:

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

You can also implement feature logic using a class:

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

Another example shows how to switch API versions based on a feature flag:

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

Extending this, you can redirect users to different API routes depending on whether they are beta testers:

<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>

Or move the logic to middleware for cleaner handling:

<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>

The possibilities with this package are limited only by your imagination, enabling powerful feature‑flag driven development in Laravel applications.

BackendPHPFeature FlagsLaravelPennant
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.