What's New in Laravel 11, Ep 02 - Missing Middleware

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] the next thing you're going to notice is that all of the middleware that has existed in the app HTTP middleware directory by default in laravel is no longer present in new laral installations using laravel 11 or greater the thought process behind this again makes sense we very rarely touch any of this middleware we leave it as is so it's been moved down into the found level and will be enabled by default but what if you want to configure some of that middleware for example the trim strings middleware takes any input from the front end and removes wh space from either end because it's almost always user input error but by default will not do so for current password password or password confirmation fields in the past if you wanted to add a field well you just edit the except array so maybe secret can be added to this array as well how would we do that in larl 11 we can make changes like that from the app service provider in the boot method we need to pull in the middleware from Foundation that we're interested in in this case it's trim strings and let's just take a look at the namespace illuminate Foundation HTTP middleware trim strings the name is identical and you'll find that trim strings has a brand new accept static method applied to it that we can use to extend any properties we want to avoid trimming when the request comes in so we would just add secret here and everything else would work just as it used to before another example of middleware that we might have changed in the past is redirect if authenticated and it was quite common practice to update this root service provider home constant so that we could change where you would be sent if you were indeed authenticated how do we do that now after all there is no root service provider so it's not going to be able to find that particular constant well again we can make use of redirect if authenticated and we pull that in from laravel not from our local project and then we can use the redirect using static method which accepts a closure that receives the request and is expected to return a route to wherever you should send somebody who is authenticated maybe that's the Home Route maybe that's the dashboard route you decide but whatever the case this is how we would configure it now instead of in our root service provider of course all of this applies to built-in middleware but what about middleware that you want to build you can use Artis to create custom middleware as before PHP Artis make middleware why don't we have I don't know log request endpoint it's going to place that middleware exactly where you'd expect in the HTTP middleware directory if we open that up well everything looks exactly the same why don't we go ahead and use the log facade in order to log some info the request URL in this case yeah you wouldn't create this middleware in real life but it gets the point across now again previously if we take a look at a laravel 10 application you would register middleware like this inside the kernel but looking at the skeleton for laravel 11 the kernel is nowhere to be found in fact if you search for kernel you won't find a kernel file at the application Level instead we can register middleware like this in app.php under bootstrap you may have avoided the bootstrap file in the past but laral 11 changes that perspective in fact app.php is where you will configure most things for your laral application inside this file you'll note we have several helper methods that set up providers routing middleware exceptions and as you'd expect middleware is what we actually want to make use of here so imagining that we want to append our log request endpoint middleware to the web middleware group I would take this middleware parameter we've been given call web and pass in the log request endpoint fully qualified class name and that's it with that in place if we visit the landing page the laravel splash screen for laravel 11 and then we come back and open up laravel dolog well I would expect yeah here we go the URL that we visited if I do it again maybe a second time and we go and look once more now there are three entries in our log so that's how we can configure middleware note that the second item is prepend so if rather than adding the middleware to the end of the web stack we wanted to prepend it you could use name arguments to do that very easily and you can also add an array here if you want to add multiple pull middleware items to a particular stack whilst we're on the topic it's worth briefly mentioning that the exception Handler is also gone in laral 11 so in this laral 10 application perhaps you've been used to using the don't flash array or registering renderable exceptions like this GitHub fail to respond exception inside the register method at the bottom well in laral 11 you're going to do all that work inside the bootstrap app.php file in the with exceptions callback so we can say exceptions don't flash and you can add any attributes there we could say exceptions renderable and we could set up our exception render call back here you can also do reportable all of the functionality Remains the Same it's just moved from a Handler to this single callback
Info
Channel: Laracasts
Views: 2,551
Rating: undefined out of 5
Keywords: web development, php, programming, laravel, laracasts, jeffrey way, coding
Id: ndq8gHA6J24
Channel Id: undefined
Length: 5min 48sec (348 seconds)
Published: Wed Mar 13 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.