Laravel Pennant: first-party feature flags

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
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 [Music] that pennant is a first-party feature flag library for laravel is originally written by laral core team member Tim McDonald and it's still maintained by the laral team it's free it's open source we're going to composer install the package here in a minute if you're familiar with something like launch darkley or Flags Smith or anything like that it's very similar to that except it all lives inside your application so Pinn it 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 Fe feat Flags so everywhere in laral 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 LEL pennant this is going to pull the package down and put it into our application here we can go ahead and clear that out and now what I want to do is say AR vendor publish so I want to publish two things the pennant uh configuration file and the migration to store the result 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 we can clear this out and say open database in table plus and we look at that we see we should see features there we go features so there's a features table there H nothing in it yet which is fine I knew that so carrying on looking over here in the configuration file you'll see pennant pphp now pinnet has the concept of um stores and every time you resolve a feature flag it's going to write the resolved value to one of the stores and this means um it's going to put it in the database or if you're in testing mode you could put it just in an inmemory 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 reddis store or you could call out the some third- party API I think the database store is probably fine um so we're going to 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 going to stick to the defaults now that we've got the package installed and the infrastructure set up the database table is built Etc let's go ahead and start defining some features so I'm going to pop over to the app service provider and in here I'm going to say feature Define and I'm going to say um what do what do we want to call let's just call it brand new feature and here I'm going to pass a closure and by default every feature is tied to a user you can change that you can change that such that every features 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 want to 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 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 Aon example.com password is password and that's not going to work CU I misspelled my own name okay good so far let's add Steve because 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 kind of 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 going to be what you need I'll show you how to do a few of those but before I do I want to 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 docomo SQL light.com Aaron francis.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 U 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 Benton now.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 going to do here is I'm just going to for this demo Brute Force login as user number one so now if we come down here and we say return off usern name 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 going to hit that closure again which means if I were to change the logic here so of name equals Fubar return true we're still going to 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 want to 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 any 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 want to choose one time so this is randomly going to decide 10% of the people will get this feature you can make this shorter by dropping out the closure Al together and just saying that one out of 10 people 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 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 want to 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 to this feature so we're going to 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 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 more 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 going to 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 grip 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 going to 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 want to 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 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 larvel pinnit makes it super easy to AB 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 kind of get out of date after a while so check this out let me know what you think if you want to watch a video about laral Octane and how it speeds up your laral application um I'll put a link here until the next time see you
Info
Channel: Aaron Francis
Views: 15,247
Rating: undefined out of 5
Keywords:
Id: EjLAaeHSPWY
Channel Id: undefined
Length: 14min 21sec (861 seconds)
Published: Wed Jun 05 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.