Laravel Pennant: first-party feature flags
In this video, I give you an overview of Laravel Pennant, a first-party feature flag library for Laravel that makes it easy to manage feature rollouts and A/B testing within your applications. We’ll explore how to set up and use Laravel Pennant, define and resolve feature flags, and integrate them into your views, middleware, and application logic for seamless feature management.
Everything I said
Here is my question for you today. What does a PHP framework look like when it is solely focused on allowing you to build real-world applications? What kinds of things might that framework provide? Well, with Laravel Pennant, we get a little glimpse of that. Pennant is a first-party feature flag library for Laravel.
It was originally written by Laravel core team member Tim McDonald, and it's still maintained by the Laravel team. It's free. It's open source. We're gonna Composer install the package here in a minute. If you're familiar with something like LaunchDarkly or FlagSmith or anything like that, it's very similar to that, except it all lives inside your application.
So Pennant allows you to define feature flags, um, to check them, put them in middleware, put them in views. There are some blade directives for these feature flags. So everywhere in Laravel that you would want to check a feature flag, Pennant provides, uh, a functionality for doing that. To get started, all we need to do is say composer require laravel/pennant.
This is gonna pull the package down and put it into our application here. We can go ahead and clear that out, and now what I wanna do is say ar vendor:publish. So I wanna publish two things, the Pennant, uh, configuration file and the migration to store the resolved feature flags in the database.
We'll talk about that in a second. So if we say migrate fresh, you'll see that this features table was created. Can clear this out and say open database in TablePlus, and we look at that. We see... We should see features. There we go. Features. So there's a features table there. Mm, nothing in it yet, which is fine. I knew that. So carrying on.
Looking over here in the configuration file, you'll see pennant.php. Now, Pennant has the concept of, um, stores, and every time you resolve a feature flag, it's gonna write the resolved value to one of the stores, and this means, um, it's gonna put it in the database, or if you're in testing mode, you could put it just in an in-memory array, um, so that you don't have it in the database for your testing.
Now, you could potentially write your own store if you wanted. You could write a Redis store, or you could call out to some third-party API. I think the database store i- is probably fine. Um, so we're gonna power through with the database store. If you want to learn more about the different types of, uh, stores you can write, you can check the docs for that.
So database is the default. You can see down here it is a driver-based system, just like everything else in Laravel. So you could potentially put it on a different connection or in a different table. We're gonna stick to the defaults. Now that we've got the package installed and the infrastructure set up, the database tables built, et cetera, let's go ahead and start defining some features.
So I'm gonna pop over to the app service provider, and in here I'm gonna say feature define, and I'm gonna say, um, what do, what do we wanna call it? Let's just call it brand-new-feature, and here I'm gonna pass a closure, and by default, every feature is tied to a user.
You can change that. You can change that such that every feature is tied to a team. You can change it so that some features are tied to teams and some features are tied to users. You can do, you can do basically anything. The docs are very thorough, so if you wanna know how to do that, you can go check that out. But please know that that is possible. Now, this is just a PHP callback, so we get to decide how do we determine if this user has access to the brand-new feature?
We can say return username equals Aaron. That seems pretty dumb, but it's good enough for now. Normally, you would do something normally you would do something like is admin. You could do, um, if they're an admin, or you could do something like is high value, high valued customer or something like that, and then they would have access to the brand-new feature.
For now, let's just stick with is Aaron. Very, very bad, but good enough. So if we were to... Yep, let's open this again, and we need to come in here and add a user. We'll just add a user, aaron@example.com. Password is password, and that's not gonna work 'cause I misspelled my own name.
Okay, good so far. Let's add Steve, 'cause Steve is not named Aaron. steve@example.com. Password is password. That's all that it takes to define a feature. To resolve a feature, you got a couple of different options. You could resolve it in a middleware, so as the request is coming in, you can kinda reroute it based on if a feature is enabled or not enabled.
You can resolve it in a view file. So if you have, um, you have a blade file and you've got these directives that will allow you to check to see if a feature is enabled, you could do it there, or you could just resolve it off of the, um, feature facade and run code based on, on that. So anywhere else in your Laravel app, the feature facade is gonna be what you need.
I'll show you how to do a few of those, but before I do, I wanna thank Bento for sponsoring this video. Bento is the email service provider that I personally use on all of my projects and have for a couple of years now. So I use it on screencasting.com, uh, highperformancesqlite.com, aaronfrancis.com. Anywhere that I have an email capture, that goes into Bento.
I love Bento for a few reasons. One is the founder is incredibly responsive and always in Discord, uh, helping people with customer support, that sort of thing. Two, deliverability is great. It's hard to get newsletters or announcements to land in inboxes, and Bento does a great job. Finally, the workflows.
I love building out these workflows to add and remove tags, and just stuff flying everywhere. I, I love it so much. I run my business out of Bento. The email list is super important to me. So go to bentonow.com. Um, tell Jesse, the founder, that I sent you. I'll leave a link in the description, and thank you, Bento, for sponsoring this video.
Now, back to resolving some feature flags. The first thing I'm gonna do here is I'm just going to, for this demo, brute force log in as user number one. So now if we come down here and we say return auth username, we should see Aaron in the browser, and if we log in as user number two, we should see Steve. Great.
Now let's check to see if this feature is active for the first user. So active, and what did we call it? Uh, brand new feature. So we'll check to see if brand new feature is active for the currently logged in user. Let's simplify that, and let's start by logging in as Aaron, and we'll return this out to the browser as JSON.
So now if we hit this, we should see true, which means for user number one, which is me, Aaron, this feature is enabled. And if we were to log in as Steve, for user number two, the feature is disabled. Now, interestingly, if we look in the database, we will see that those features have been written to the database. So now that those features have been resolved for this particular user, we're not gonna hit that closure again, which means if I were to change the
logic here, so if name equals foobar return true, we're still gonna get true for Aaron, because it's already been resolved for Aaron. This is great when you're assigning features randomly, right? Because you don't want it popping back and forth between on and off. You wanna assign maybe 30% of the people to a certain feature, and those people need to stay in that group that has the feature, not bounce back and forth.
And so that's what these stores do. You resolve the feature once, and then you write the value to a store, which you can later clear, but that keeps it consistent across many sessions. Let me show you how we can use a lottery to decide who gets a feature at random. We could do something like lottery odds of, what's the, one out of 10?
So we'll do one out of 10 choose. And so we just wanna choose one time. So this is randomly gonna decide 10% of the people will get this feature. You can make this shorter by dropping out the closure altogether and just saying that one out of 10 people gets the brand new feature.
So let's come in here, and we'll delete these and come back to the browser. So I didn't get it this time, which means it will be written... It's written to the database, and if we come back and change it to Steve, let's see if Steve gets the brand new feature. Also no. So using the lottery is a good way to evenly distribute, um, amongst a, a big population, where you don't really care who gets the new feature or who doesn't.
Let's look at using a middleware really quickly. So we can say that on this route, there is a middleware of ensure features are active using, and we wanna say, brand new feature here. So here, what we're doing is we're saying, "This person can only access this route if they are a part of this cohort, if they have this feature enabled." So we can then say, um, let's use double quotes, you're in.
So neither Steve nor I are currently opted into this feature, so we're gonna get a bad request, because this, this route is protected by this middleware. So if we go into the database and we change this value to true, and we come back, you're in. So you can see that you can use the middleware to protect routes to make sure that the people have a certain feature enabled for their user account or their team account, or however you're doing it.
Let's take a look at defining features that are not necessarily true/false. So if we had a feature that was like, uh, I don't know, theme, and we're testing out some new themes, and it receives the user, but in this case, we probably won't use it. We can just say return array random from, what would our themes be? We could have a dark theme and a light theme and, let's say, a mono theme.
So we can just return a random value out of that array and assign it to that user. So if we come back into web, we can get rid of this middleware for now. We don't need that anymore. So we'll get rid of that, and we'll say, um, let's look at the feature value for, I think we called it new theme. I can never remember anything. We just called it theme.
Told you. So if we take the feature value for theme, and looks like user number one got light mode. What's user number two gonna get? Dark mode. So this is a way that you can store or, or, um, partition your users on more than just true/false, and if we check in the database, you'll see, there we go, the string light and the string dark have been stored for these particular users.
Now let's say you change the resolution logic, and you need to clear everything out of the database. That can be kind of a pain unless you use... Let's look for, let's grep for pennant and see what's available. So pennant purge looks right. Let's say, um, PHP Artisan pennant purge, and then we'll say theme.
So theme purged from storage. Now theme is gone, and anytime a user, um, comes back in, they're gonna be reassigned at random a new theme. And so that's a good way to clear out the stuff that has been stored in the database. The last thing I wanna show you is resolving features in a Blade file, which can be super helpful. Um, so let's do view welcome and hit that.
So there's our welcome view, and if we pop open- If we pop open welcome, um, let's just take all of that out, and we can get rid of all of that and just say, "Hey." Hey. Great. Now, there are a couple of ways you can do this. You can say feature brand-new feature and end feature.
Hey. Well, that's confusing. Um, you're, you're in the brand-new feature. So user number one is in the brand-new feature, and if we were to change this to false, you would see that it would go away. You can also do, uh...
You can make it an else. You're not in the brand-new feature. So you see, you can use the feature else end feature to define or to, to show things based on if they're in the feature. If it is, like we had earlier, if it's a feature where it has values and it's not just true/false, you can say, um...
You can do stuff like this. So you have feature theme light else dark, and you can, you can do your whole branching logic here. So we'll say, um, else... What is it? Mono dark mono. So now if we hit that in the browser, that user has dark, and if we change it to user number two, that user has light.
Laravel Pennant makes it super easy to A/B test new features or gradually roll out features or put, put your internal users on a new feature and kind of test it in production without having long-lived branches that kinda get out of date after a while. So check this out. Let me know what you think. If you wanna watch a video about Laravel Octane and how it speeds up your Laravel application, um, I'll put a link here.
Until the next time, see ya.