Feature flagging is a great technique to release code, and in my current role it’s something we simply do all day, every day. So, I thought I’d go over what a feature flag actually is, how you can add them to a Laravel app (with or without a package), and why I think they’re so good.

So, what is a feature flag?

A feature flag wraps all of the new code you’re releasing… so, imagine you’re adding a new button.. that new button would be wrapped in an if/else, and if the feature is active.. then the end user will see the button!

How to add feature flags in Laravel

Laravel offers the Pennant package to do this, but, if you need something simple.. you don’t need the package at all. You can create a features.php in your config directory, and manage the flags yourself… such as…

<?php

// config/features.php

$staging = env('APP_ENV') === 'staging';

return [
    'new_button' => $staging,
];

Then, wherever you need it.. just check the config!

if (config('features.new_button')) {
    // Show the shiny new button...
}

// You can create helpers yourself to tidy this up too...

Though, if you want all the bells and whistles I’d generally just go with Pennant, but I wanted to highlight there are alternatives :)

Great but.. why use feature flags?

Well, the big one for me is that it separates deploying code from releasing it. Your code can be sat on production for days, even weeks, and nobody sees it until you flip the flag.. which takes a lot of the stress out of release day!

It also means you can have confidence in the whole application.. if you have a staging site and a production site, they can be running the exact same code side by side, but staging has the flag enabled and production doesn’t. So both sites run all the same jobs, queues, etc.. and you’re testing the real thing, not some long-lived branch that’s drifted miles away from main.

Speaking of branches.. that’s another win. Rather than one massive PR that sits open for weeks (and all the merge conflicts that come with it), you can merge small PRs often, all hidden behind the flag. Smaller PRs are easier to review, and much easier to reason about.

And if something does break, it’s simple to switch off.. just set the flag to false, push the change and it’s back to the old code. No reverting commits, no wrangling with commit history, and no figuring it out manually at 5pm on a Friday :)

Once you outgrow the simple config approach, that’s where Pennant really shines too.. you can roll a feature out to just your team, or a handful of customers, before everyone else gets it.