Laravel Middleware - A Complete Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello and welcome to tech tool india in this video i am going to explain you about laravel middleware after watching this video you will able to understand all about laravel middleware if you are new to the channel please do subscribe our channel to never miss any video in the future let's see what we will cover in this video in this video we will see how we can create the middleware how we register the middleware how we can group the middleware and how we can send the response on a middleware and how we can pass a parameter to any middleware so without wasting any time let's jump into the code and see the example so i have already installed laravel 9. if you want to see how we can install level 9 you can watch the video i have put a link in description so let's understand first what is middleware middleware basically is a bridge between request and response you can understand like this every request when it comes to our application will pass through that middleware as it's suggesting by the name it will be a bridge between request and response by saying that let's jump into and see how we can make any new middleware so to make a middleware we can run a command php artisan make middleware and the name of middleware let's say i want to check the request year so i can name it like check year let's hit enter as soon as you hit enter it says middleware created successfully so where actually middleware create it's inside the app folder go inside http there is a folder called middleware if i zoom it little bit so you will see we have made check ear middleware so it created a check year dot php file let's open this as you can see it makes a check ear class and it has a handle function which accepts request and a closer which is next so by default what it does it actually pass any request coming to this class to the next request right so here in the handle function we can write our logic based on what we want to do let's say we want to check it if this is coming this middleware is working or not so how we can do that so in order to use this middleware we have to register this middleware for registering we need to open this app inside app folder there is http folder inside the http folder there is a kernel.php file let's open this file as soon as you open this file so there is a middleware groups there is a root middleware so as i want to use this inside our root what i'll do i'll just register our middleware here i'll call it check here path here app http middleware check here and class so by defining this we have already registered our check year root so let's open our roots web dot php file let's go into roots folder open web dot php file as we already defined uh use last uses slash create root inside our user controller you can take any route to verify your middleware i am just taking an example roots you can create any other as well so as you see if i go now on this class user slash create as i paste slash user slash create in a browser let's go to this it actually loading a sample page so let's go into code right now what how we can apply a middleware we can give it a middleware function here and the name of middleware is check here right so we are passing it check here middleware let's refresh this so it's giving an error let's go here we need to give it a slash app slash http let's refresh this now it's working so as we are not doing anything in our check ear middleware let's say i want to see the request what is coming inside a request so i just dump all the request by request of all and then dump here let's see refresh the page you see there is a blank what if i just pass here a year of 2022 so if you see now it has a year of 2022 let's say we want to verify if the request has year 2022 then it will pass the request to another next request otherwise it will return to landing page let's apply a condition saying if request has year and a request of year is equals to 2022 right if this pass both the condition then it will go here otherwise it will redirect to to a root of so let's say we have this welcome root i'm just giving it a name of welcome right so we can call this root here welcome okay that's fine let's remove this dump here now if we test our page as soon as i refresh the page it goes to as soon as i refresh this it goes to this particular view page let's try editing this to 2021 what it does it actually redirects to welcome page again if i go on 2022 and hit and refresh it it loads the piece so you see how we can use a middleware to prevent any request for you know to go inside and perform the action before that we can prevent the request so as you see by just applying a single middleware we can prevent a request to go to the controller and written a response from here inside a middleware itself so that's how we can create a middleware and return the response let's go into our kernel.php file and let's understand few other here so this is how we define the root middleware what if we want to declare this middleware to be a global middleware so for that we want to declare it here inside this middleware array and the next sometimes there is requirement of creating a middleware group for example all the apis should pass through a some middleware or all web requests should pass through some middleware in that case what we do we actually create groups and in groups what we define we define all the required middleware by just applying a single group we can apply all the middlewares to that particular group that will reduce number of middleware written in the web for example let's say we have auth middleware and we have check here how we can apply multiple middleware on a single route so how we can do we can simply pass multiple array here by just passing a array inside our middleware function this will allow us to pass multiple array let's say if we want to take some input inside our middleware right so what we can do let's say if we want to pass the year parameter inside our middleware and for example let's say two particular routes and let's say user slash new will be one which will accept 2023 and the one which will accept 2022 so how we can manage these things to pass to create a single middleware and pass a different input variable for those so for that what we can do so this handle method currently accepting two parameters one is request and second is closer for our response let's define here a new parameter which will be a string and let's name it a year right so after next closer we can take this as a year parameter and instead of this 2022 what we can do we can actually pass a year here and that's it that's how we can make our middleware to be a more dynamic single middleware can be used for multiple purposes so let's see if this is solving our problem or not let's go into browser for and what we have applied the condition for slash uses slash create the ear should be 2022 and slash user slash new the year should be 2023 so let me copy this url as soon as i go this this is loading that means it is working fine if i try to use 2023 here it is actually redirecting to a landing page let's go and instead of create let's pass new and pass the year as a 2023 it's working again so you see user slash new is accepting the parameter 2023 and user slash create is accepting a parameter 2022 so that's how you can use middlewares in your route and inside the application to prevent any http request to go inside the application before that you can validate that request whether it's be in the api call or a web call both you can validate and after validating if that condition is true then you can pass the request to go inside and perform the action otherwise you can always written a response to that particular request i hope this is quite clear to you about laravel middleware if you have any questions you can put your questions on the comment below if you like this video please do subscribe our channel and get updated for any other video on the laravel till the next video keep watching keep learning thank you for watching [Music] you
Info
Channel: TechTool India
Views: 5,374
Rating: undefined out of 5
Keywords: laravel middleware, laravel, laravel 9, middleware in laravel, laravel 8, middleware laravel, laravel 8 middleware, laravel tutorial, laravel 9 tutorial, laravel middleware authentication, middleware, laravel global middleware, laravel middleware in hindi, laravel middleware tutorial, laravel middleware explained, laravel 8 tutorial, middleware laravel 8, middleware in laravel 8, laravel cors middleware, laravel 9 middleware, laravel 9 middleware uses
Id: I2N1jAz8nxY
Channel Id: undefined
Length: 12min 38sec (758 seconds)
Published: Thu Jun 09 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.