What's New in Laravel 10 - Introducing Laravel Pennant

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome back all right next up on the list is laravel pennant this is a first party package which means yeah it doesn't ship with laravel by default instead you have to install it as a package but we call it a first party package because it is maintained by the laravel core team okay so let's have a look I think you're going to like it I will Begin by installing it through composer composer require laravel pennant all right next up let's publish the configuration files so I can say vendor publish and yeah here's all the tags for a fresh application in our case here's the provider and the number is five so let's do that now all right so we can see that we have a new config dependent file as well as a new migration let's go ahead and run that migration however right now you'll see that because it's a fresh install of laravel we don't yet have a database to go along with it but again as we talked about an episode or two ago in recent months larifelle added support for automatically creating this database if it does not yet exist so let's do that now okay so here we have our usual tables but also this new features table and yeah this is what laravel pennant is it helps you maintain and manage your various feature flags and it was contributed by Tim McDonald who works on the laravel core team okay so now I get it your next question is well what's a feature flag all right think of it like this and I guarantee you've run into this many many times around the web have you ever been in a situation where you wanted to test out maybe a new style or new layout or a new page or a new dashboard or anything like that but you weren't yet ready to roll it out to everyone instead maybe a small percentage of your faithful users can try it out or maybe your users can opt in to try out the new dashboard or maybe administrators or people who work at your company can try out the new dashboard or the new design or layout but everyone else still sees the old layouts or the old API or whatever it happens to be have you ever found yourself in that situation and if so well you're working with feature flags and this package can help you all right so let's have a look I'm going to go into phpstorm I'll visit my app service provider and let's define our first feature I will say feature laravel pennant feature and let's define a new one the first argument here will be some kind of identifier that represents and describes the the feature itself I will call ours you know new design whatever format you want next we'll have a closure here that will determine whether or not each individual user has access to this feature or if the feature is active for each individual user so if I return true here oh yeah that would mean everyone has access to this feature and I don't know I'm not sure how useful that would be I don't know maybe as a toggle if you want to say all right well for the next couple of hours let's turn on this feature for everybody but then I want an easy way to quickly turn it off yeah maybe that would work but more practically and more realistically you will determine authorization and really that's what this is it authorizes a particular feature on a per user basis yeah so more practically you would say well these users can access the the new dashboard or whatever the feature is but these other users cannot access it so again it would be contention on a per user basis okay so maybe what we could do is type user and then you can inspect this user to determine if the new design feature should be active for them and yeah maybe if you have a really small site you can just do something like this like the user with an ID of one is the owner or the administrator they can see it but nobody else can this would be fine another option maybe a little more realistically is you have some kind of method on your user model maybe it's is admin is moderator um maybe you know whatever you want whatever makes sense for your application in this case I'm just moving the logic here maybe that's all you do here to Define an admin for your application maybe you're checking a roles table maybe there's a column on the user uh the users table that you would check whatever you want either way you can delegate like so return user is admin okay so I think we're ready to try this out I'm going to go into my routes file and let's see we have a again a fresh application this loads the welcome page all right let's see um yeah how about this we will say for demonstration purposes it's kind of lame but it'll work if the new design feature is activated for your account we will load this style tag here and that seems to inline the Tailwind Library so I guess otherwise you get a non-styled site so it really is a new design that you're testing out okay let's give it a shot so when you pull in the pennant package that also includes a new blade directive called feature and you can use this just like all the blade directives you're used to in the past so we're going to give it the identifier ours was new design and then don't forget to close out the block and feature okay so let's think about what this says if the new design feature is activated for the current user or for everyone only on that condition should we render this style tag that loads the Tailwind Library otherwise we don't okay and what is the logic again just as a reminder the logic is if you're an administrator you can see it otherwise you don't see it all right let's give this a shot in the browser so I give this a Reload and it should be unstyled and good I did it correctly we get a totally unstyled splash page and this is the current design I guess at least for the example okay but now let's set an authenticated user so here's what I'll do I will open up table plus and yeah here's the laraphal10 database but you'll notice that I don't have any users at the moment so here's what we can do let's open up my database Cedar that you'll find in database seeders database seater and yeah they have a little snippet here that I can uncomment so when I run the DB seed command this will create 10 uh dummy users and that's what we need okay let's give that a run PHP Artisan DB seed all right and now if I switch back to table plus and give this a refresh yeah now we have 10 uh dummy users that we can work with Okay so let's decide that Bo Sanford is the administrator all right if I switch back I'll just load this in the routes file in real life you might have an authentication system you might pull in layer full Breeze but we don't have any of that so I will temporarily Define it here I can do that by saying auth and I could say login using ID but why don't we stick with Once using ID it does the exact same thing but it doesn't it doesn't set any session or cookie so it's great for for just little quick uh test examples so this means log in the user with an ID of one but don't create a session don't store a cookie nothing like that all right so let's come back and give this another refresh and it works very cool all right so now with very little effort really we've designated that administrators can see this new design but everyone else still sees that ugly splash page the the unstyled swash page okay very cool but again my next question would be but how is this working well let's have a look if I come back to table plus and visit the features table you'll see two records here for this particular feature notice the scope one of them is user slash one so of course that's the user with an ID of one and the value is set to true because for that scope that particular user can see the feature or the feature is active for that user this other one laraphal null that would be like your Global scope guest users things like that by default that is set to false okay so that means let's try this out what if I log in the user with an ID of 2. well I want you to notice that if I come back to app service provider this logic here this closure is never triggered until you you check the feature so for example I want to make this Crystal Clear if I don't have any of this here if I never check the feature using that directive well that means this closure will never be called so if I come back and give it a bunch of refreshes and go to table plus notice we don't end up with a new record because once again you you don't call that closure you don't run that closure unless the feature is being checked okay so back to phpstorm let's bring this back and now if I come back to Firefox and give it a refresh we are checking the feature which means there will be a new record in the features table for that particular user so in this case the user with an ID of one has it turned on but the user with an ID of 2 who is not an administrator has it turned off okay but now the next thing I want to show you something you might run into is what if we decide that the user with an ID of 2 gets promoted and now they are an administrator or a moderator so you might do something like this is fine or if you won an array this doesn't matter why am I doing this but check the ID and see if it's one or two and those are your administrators okay we'll now notice that in our routes file the user with an ID of 2 is logged in but they will not see the new design even though they do qualify all right this is a key thing to understand if I switch back access for this particular user has already been saved or stored and what this effectively means is this closure is not re-running for every single request it wouldn't make sense instead it has already been cached in the database so for example if I were to delete this outright and then retry we come back give it a refresh and now we recalculate it so that means we hit this closure we determine if the user is an administrator now they are and that value is then saved or cached in the database as you see there but yeah otherwise in situations like this how might we deal with it well one option is to do what I just did manually delete it it's fine for small stuff but otherwise yeah more practically you could whenever a relevant action takes place you could activate the feature for the user or or update its status so I could say feature activate and then I provide the name of the feature new design so yeah if the user does something if they're upgraded as part of that you could run code like this just keep in mind what this code says is activate the new design feature for the current scope and in our case the the scope is the user so for the current user this does not say activate new design for everybody that visits the application it is still scoped so in our case notice that the user with an ID of 2 does not have access but when I load the home page and that user is signed in we activate it for them alone okay and actually just to make this Crystal Clear let's cache this and save the user with an ID of one is false just to show you that we're not in a blanket update changing every single record okay come back give it a refresh and now come back to table plus you will see that switch to true all right very cool so what this means is with minimal effort you can define various features in your application one could be a new design one could be how about this how about um you know once again to call it anything you want uh fresh dashboard or how about dashboard V2 or whatever naming convention your team comes up with and we can say once again you can access version 2 of the dashboard if uh the current scope the user here meets some kind of criteria or what you sometimes might want to do is say well I don't care which users access it I just don't want everyone accessing this at the exact same time well in those situations you can range for a lottery how about one out of 10 users or one out of five users can access the new dashboard and as always laravel has our back we can pull in illuminate support Lottery and let's declare our odds I said one out of five there you go add your fraction there and you're all set okay so now I don't even need this scope and I think we're all set let's go to my routes file and let's declare a dashboard and I'm just going to return a string here dashboard but then I'm going to have the new dashboard so we'll have one here all right so now we have the old dashboard and the new one that you want to load okay so here you might check if the what did we call it dashboard V2 feature is active maybe we redirect them somewhere else that would be fine redirect them to the new dashboard otherwise stick with the old dashboard all right let's give this a try let's visit our dashboard and we get the current dashboard so if I come up to table plus we should have a new record here and sure enough the user with an ID of 2 as the odds turn out uh did not have access and actually on that note just to make this a little easier on myself why don't we say three out of four chances a 75 chance that we will get the new dashboard okay so let's come back and hopefully this won't take too long we switch our current user give it a refresh there we go it did work okay so now we can see a relationship between any scope in this case a user and all of the features associated with it so in this case the user with an ID of one does not have access to the new design but they do have access to this dashboard V2 all right so now we can see that the user with an ID of 1 has access to this new dashboard but yeah the user with an ID of 2 will not so if we give it a try they're still stuck with the old dashboard but now you'll notice because we are redirecting to this new URI and there's no authorization associated with it well yeah they were still able to access that new dashboard okay so that's another thing we should check for and again we can solve that problem I will visit my HTTP kernel and yeah we can add a new one that's available through this package so here is our route middleware aliases we're going to call it a feature and that will be ensure there it is features are active and real quick if we have a look there here's the middleware notice that you can pass in the name of the feature okay cool this is all we have to do so now for any pages that need protection you can use your new middleware middleware feature colon and then the name of the feature dashboard V2 all right cool so now yeah if I come back and refresh we don't have access to this page so an exception is thrown and of course there are options to handle that exception however you want to okay so there's a lot more to dig into here but I think this will get you started it's a really nice addition it's very elegant I think Tim did a great job so I encourage you to check it out
Info
Channel: Laracasts
Views: 15,732
Rating: undefined out of 5
Keywords: web development, php, programming, laravel, laracasts, jeffrey way, coding
Id: 0huthUNaZ08
Channel Id: undefined
Length: 16min 56sec (1016 seconds)
Published: Fri Feb 17 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.