Playing
13. Conditionable trait added to routes

Transcript

00:00
The conditionable trait has now been added to the root object in Laravel. Now what does that mean? What even is the conditionable trait if you've never seen it? Well let's dive in first and then I'm going to show you a couple of examples where I would use
00:13
this personally. There is already a really good example of this over on the pull request, but I'm going to look at two everyday examples that I would use probably quite often. Okay so first of all let's dive straight over to the root object. So let's open up this under rooting and root and you can see here sure enough we have this conditionable trait. Let's go ahead
00:34
and open that up and see what we've got. So we have a when method. Now if you've used Laravel for a while you'll know that a lot of things you can tack when onto the end of and that will go ahead and allow you to under a certain condition manipulate this object within the callback. So have a look through this, we're not going to go through everything here, but
00:54
let's head straight over to the root object which we get back when we go ahead and build up a root from this facade and let's see what we can do with it. Okay so let's go and build out a simple example here. Let's say that we had some sort of endpoint so let's just call this middleware just to knock out this example and we'll create out a just a short closure to return the text middleware
01:17
just to keep things super simple. So let's head over to that root and there it is. Now let's say that we had some middleware that we wanted to add onto here but let's go ahead and build that middleware out really quickly. So I'm going to make this middleware I'm going to call this production only middleware. So let's go ahead and create this out maybe it does something that you don't
01:36
necessarily want to happen in local development. Well let's go ahead and apply this middleware and let's put that in there so production only middleware and let's actually head over to there and do something in here so we can see it's actually doing something. Okay so you can see we get production only and then middleware which is what we're returning from there.
01:58
Now let's say that we don't want this production only specific middleware to be run in a local environment. What do we do? Well previously we would have had to do something like this some sort of condition inside of here register this otherwise doing this. I don't even know if this is going to work. Yeah it does work but obviously doesn't look great. So what can we do
02:19
now? Well because we have that conditionable trait on our root object we can now say when. So what's the condition? Well we're going to say app is local or you could do it the other way around and say when app is in production we want to add this middleware. It doesn't really matter. Then we have a closure in here. Now what do we get passed into this closure? Well we get whatever
02:40
this is applied on. Whatever this trait has been applied on we get back in here which happens to be a root object. Now what we can't do is type in this because we've already got the facade pulled in here. So let's go ahead and just manually pull in the routing and router or root and let's just call this root object. I can't think of a better name for it at the moment but we'll type in that
03:03
under root object and we'll pass the root in. So now that we've got this back this closure will only be run when this is true. So that means that when we are in a local environment we can continue to chain on much like we would be able to do here and do something like without middleware. So we can do that on there but now we can do it only within this closure under this condition. So we
03:24
can say without middleware and we can say production only middleware and we should be good. So when we go ahead and give this a refresh we get production only middleware. I'm currently in production over here so let's change this over to local give that a refresh and sure enough it disappears. So this is just one example but you can manipulate your route in any way you want
03:45
within whatever condition that you need. This condition could be absolutely anything and then you can just continue to build up or remove stuff from your route. Okay so another example that I would probably use quite often is to do with rate limiting. So let's go ahead and create out another route here and let's say we have a limited route and again let's just create
04:06
our short closure here to return some text. Head over to this in the browser so say limited and let's go ahead and apply the throttle middleware to this to throttle this under a certain amount of requests. So let's say five requests and by default this will be every one minute. So obviously if we go ahead and give this a refresh multiple times eventually we're locked
04:28
out the 429 status. Now that's fine and perfect for production but obviously when we're locally developing this is probably going to get in the way and it's going to mean we're going to have to clear out this throttle manually it's going to be a massive pain. So what do we do? Well we can go ahead and say when an app is local let's go ahead and get rid of that middleware. So again we can go
04:49
ahead and pull in this closure in here let's put in that root object which I'm sure we could think of a better name for at some point and let's go ahead and get rid and exclude this middleware. So let's say without middleware now with this we have to provide the exact same name and properties we'd have to say throttle five here but that should work. So if we head over to env make sure this is
05:10
local and give this a refresh there we go now on a local environment this is not throttled. So we've looked at an example of doing this just with middleware but you could do this with absolutely anything. So next time you have something that you're chaining onto a root and you want to go ahead and change it under a certain condition this is now conditionable so you can do that
05:30
very very easily without wrapping stuff in if statements or moving stuff over to your controller constructor. Now there's another way that you can do this so just to give you a really good idea of this let's say that we wanted to do exactly the same thing as this but only throttle this in production. Well in that case we can make this a bit clearer so we could take this middleware here
05:53
and we can say app is production and then we can only throttle it in production so let's say root middleware throttle five so that's obviously a lot cleaner it just depends on your needs and how you prefer to write code but obviously that's going to work in exactly the same way we're just switching around the conditions and that gets rid of an extra line of code.
06:13
So a really cool addition by just adding that conditionable trait to the root object we now get more control over building up roots with a simple chain of the when method. Of course we've looked at two very simple examples here just relating to middleware but you can go ahead and apply this to anything that you would usually build up when you access the root object.
26 episodes2 hrs 34 mins

Overview

Need to know what’s new in Laravel as it happens? Every episode of this course is dedicated to covering the most interesting and useful Laravel additions in detail, so you’re ready to start using them in your applications.

Check back often, and stay completely up-to-date with Laravel.

Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!

Episode discussion

No comments, yet. Be the first!