Laravel Pennant: Strong Feature Tags in the Easiest Way

Laravel Pennant: Strong Feature Tags in the Easiest Way

laravel pennant

Summary

  1. Introduction
  2. Installation
  3. Configuration
  4. Setting Up A Feature
  5. Conclusion

1. Introduction

Feature tags provide a flexible mechanism for categorizing and managing application features based on tags. You can easily enable or disable specific functionality based on a specific logic. This is particularly useful for gradual feature rollouts or experimentation. Laravel 10 brings a new feature called Laravel Pennant. Now we can control the access to features by using Feature Tags easily.

2. Installation

Install the package

composer require laravel/pennant

Publish the configuration and migration

php artisan vendor:publish --provider="Laravel\Pennant\PennantServiceProvider"

Run the migrations

php artisan migrate

3. Configuration

In the config/pennant.php, we can change the value for the PENNANT_STORE. The default values is database, but you need to be aware that if you use database you’ll need to handle it every time the permission for any user has changed. For this post I’ll use array, because when the permission changes, it applies automatically since it resolves the permission in runtime.

'default' => env('PENNANT_STORE', 'array'),

4. Setting Up A Feature

Supposing we want to create a feature only for premium users, called TopSellerProducts.

Feature

Create the class for handling the feature

php artisan pennant:feature TopSellerProducts

Put the code into the resolve method to determine if a user can access this feature. For this example, we’ll check if the user is a premium user. But another very common use for feature tags is roll out new features available only for admins. For this case, we’d only change the rule for return $user->isAdmin() and when the feature is tested and approved we’d change the rule to all the user can access it:

<?php
 
namespace App\Features;
 
class TopSellerProducts
{
    public function resolve(User $user): mixed
    {
        return $user->isPremium();
    }
}

Model

Since we’re going to check if the User has permission for that feature, we can add a Trait to User model, to make easier to check if they can access the feature:

<?php

namespace App\Models;

use Illuminate\Foundation\Auth\User as Authenticatable;
use Laravel\Pennant\Concerns\HasFeatures;

class User extends Authenticatable
{
    use HasFeatures;
  	
  	public function isPremium(): bool
    {
      	return (bool) $this->is_premium;
    }
}

Policy

We can create a policy for Product, checking if the authenticated user has permission to access that feature:

<?php
 
namespace App\Policies;

class ProductPolicy
{

  	public function topSellerProducts(User $user): mixed
    {
        return $user->features()?->active(TopSellerProducts::class);
    }
}

Controller

And now, in the Controller, we can use the method authorize, to determine if the authenticated User can access this endpoint to list the top seller products:

<?php

namespace App\Http\Controllers;

use App\Models\Product;
use Illuminate\Http\Request;

class TopSellersProductsController extends Controller
{

    public function index(Request $request)
    {
      	$this->authorize('topSellerProducts', Product::class);
      	
      	return response()->json(Product::topSellersProducts())
    }
}

Route

For our route we’re gonna have a GET endpoint to list the top seller products.

<?php

use App\Http\Controllers;
use Illuminate\Support\Facades\Route;

Route::get('/products/top-sellers', [Controllers\TopSellersProductsController::class, 'index'])->name('products.top-sellers');

5. Conclusion

feature tags in Laravel bring a new level of flexibility and control to your application development workflow. With feature tags, you can categorize and manage your application’s features based on specific permissions and rules, allowing you to enable or disable functionality dynamically without modifying the codebase, even the database if you use array as the default storage. This capability is particularly valuable for gradual feature rollouts and experimentation, and for segmentation.

You can checkout the documentation here => Laravel Pennant.

I strongly recommend you read this post about How To Create Better PDFs With Laravel Easily.

Share this content:

Robson Sousa

I’m Brazilian, married to Ludmila and Júlia’s father. I have lived in Timon, Teresina, Uberaba and now Goiânia. I had my first job as a Software Developer at 2010. From there to here, I have met so much talented people, learnt a lot from each different experience and collegues, and shared knowledge.

Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments