Laravel 10 - How to create and use Middleware in Laravel | Laravel middleware tutorial step by step

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome back so guys in this video we are going to learn about middleware in larell So guys what is middleware so I have written in a simple words like what is middleware so middleware is a filter for HTTP request and also the middleware will check the request before reaching your main logic in the controller okay so now guys let's get back so guys let's get back to our editor so here we are and now guys let's let us first step what you have to do to create a middleware so it's a very simple command guys that is your Artisan command just let me just show you um PHP artisan make colon Middle where name admin middleware okay middleware so we are taking an example of admin right so hit enter and here your middleware is created let me just minimize and go to our app HTTP and you'll find a middleware folder inside that middleware folder you have many other middleware but what we have created is admin middleware so you can find that admin middleware PHP file and this is how our middleware looks guys by default the larel provides one handle function and which is about the request process okay so guys whatever logic we are going to write we are going to write in this handle function itself so guys let me just uh tell you with a example like here is my categories and these are my products like it is accessible right now without logged in user okay so what I want to do I want to check whether the user is logged in or not and also he admin or not so in that case we can use middleware so guys now let's get back let me close this terminal and yep so so first step you can just put a if condition here and just return like in the if condition Au check function so what does this do it checks for authentication whether it is authenticated or not user is authenticated or not let me import the class so here it Imports so guys once it is authenticated then you can process the request if not authenticated then you can tell about 41 which means un not authenticated not about it's a o r t about okay let's save and now guys if you have created a middleware you have to register the middleware so here you have a kernel.php so guys one is your middleware and then middleware group and a middleware aliases okay so guys totally there are three middlewares here so each middleware has different Logics if you're going to apply the middleware here it is going to be globally applied okay it's all over the page and before the request goes so guys we will be using that here in the middleware aliases so now let's create one uh key like is admin I want to check whether that you authenticated user is admin or not and here you can call the path of your middleware what you have created so let's go back to our middleware and here is my name space I will just copy this and get back give a back slash and paste here forward slash then your middleware class colon colon class which is admin middleware that's it so guys our admin middleware is registered in kernel.php and here is our key that is is admin so we are going to use this is admin in our route or controller or anywhere okay so our middleware is registered and now let's close this kernel.php perfect so guys now let us uh go to our routes and go to your web.php and what we were checking like on this products view so let us see on this when you call this URL you have to check the middleware you have to apply the materal Weare on this route so let's get back and see for the product route so here is my Complete product route guys and let me just zoom it more so it will be more clear so guys there are many ways to include the middleware let's see with the first example like route colon middleware function and inside this you'll paste that is admin and then you have to group this and utilize for that you can use a group function G U P group function function open and close and open and close calias hit enter let me just and now you can just controll X and paste after your route ends so this is my Complete product route so I'm pasting inside the middleware okay so this is the first process of using middleware and now guys let us check whether this middleware is working or not so yep let's move and if you refresh here it's selling 41 unauthorized which means like it is not authenticated so you have to login First to access your uh products details so now guys let us login and see whether we can access it or not so for that let's go to the login and login first so let me see whether we have a record or not so here we have some record let me copy this and get back and paste the credential and let's click to login perfect so we have logged in guys and now let me go on the homepage so just move on the homepage and now try to access your products perfect so you see guys that you are able to access your products data okay so now guys let's get back to our editor and now guys let me just show you in another way also like this is a complete a route group I mean group The middleware and use that for every route so now let me just comment this and just copy this middleware and paste at last of your each route so you paste here okay and now if you refresh you are currently logged in so you are able to access let me just show you this same page in the incognito mode so control shift n and hit enter here you see 41 unauthorized okay so you are not able to access but if you try to access other Pages like products create you'll be able to access because you have not given the middleware right so let me just show you about products create hit enter so you are able to access once you go to product it will tell like not authenticated which means unauthorized let me close the incognito mode and this is one type of middle guys and also let me just show you out here in this middleware if you want to use multiple middle Wares like is admin super admin or guest anything so you just need to mention that in a square bracket as a array and just give single codes comma and separate them and use it so what we are doing in this middleware admin middleware we are just checking this Au okay so larel gives you by default One AU middleware so let me just show you that here in the kernel you have a Au middleware you can utilize this so now you can get back and paste here and here you can change the logic of something else so now let me just show you okay refresh so right now I'm logged in so it's able to access both the middleware and working fine so now guys uh let me just remove this let me comment this code and just return turn the true and refresh so it's working fine but now if you just move here in the products it reducts you to login page because that authentication code is written like that so this Au you are using if you just remove this uh is admin whatever we have given and if you try to access your products page so let me go to the products hit enter it will redirect you to the login page okay so we can utilize this middleware Au for the authentication checking part done so now guys coming back to our example which is is admin so we have to check whether the logged in user is admin or not yep so for that uh let me first go to the dashboard and log out here so let's log out first okay now if you try to access products so here you redirect back to login let me remove this Au so we can go according to our example so yep products okay so we have changed the condition here let's uncomment everything and about 41 if is not authenticated okay so we have logged out and you are not able to access perfect guys so guys coming back to our admin check let me me create a migration file to add a column in the user table which will be as role okay so PHP Artis make colon migration add rooll field to users table okay hit enter and now you can go to your database migration app and at the last you'll find that migration file so here we have up function and down function so inste set this let me us a table of then after function comma again then function open and close open and close CBR done and let me give a semicolon here so after what column I want I want after remember token so let me just copy that column name and paste here so after this remember token I want to add a column which is going to be a Ware of string and the name will be R okay and here we are getting the table let's pass through this function and you'll be able to access so when rule creates by default I want to make him as user okay so whenever new user is registered he will be registered as user rule okay and now guys when you are in the down function you can add this like Drop column whenever you do migration refresh or roll back so at that time it is helpful for this so drop column Ru so same name what you have given here in the up function you have to provide in the down function name that's it and now guys let's migrate this file into our database PHP Artisan migrate hit enter role field is Mig now you can get back so here if you refresh you'll see that one R column has appeared by default you'll be seeing that you have rule as user cool so now guys let me just live as user itself and let's get back to our editor and get started with the admin middleware so guys first we are checking it is authenticated not if it is authenticated you're allowing him so now we need to check if the user authenticated user his role is double equal to admin then you need to allow him else just about him you just need to tell like you are not able to access like 4 not3 which is going to be forbidden you have no access for this okay let's save and now guys let us get back and refresh so first you see that it is 41 you are not logged in so let me just login here with at gmail.com password 1 2 3 4 5 6 7 8 hit enter and you're logged in perfect so guys after logging you accessing the products page so while accessing the product page it is saying 43 forbidden okay which means you are not allowed to access this page so for that it is throwing this 43 now let us make the user Ro as admin okay so just hit enter admin and here I have updated to admin okay now if you refresh you'll be able to access cool guys so this is how we use our middleware in LEL okay and here guys I have just set up with the simple so if you're using a spacy package for roles and permission you can just check like if the authenticated user has a rule of admin or super admin this is how we check okay if you're using a spaceship package or creating a custom function in your user model so I just want to give a clear example like how to use a middleware and now guys let us see with the last example how we can use this uh middleware inside our controller part like you don't want to give here in the route middleware but instead you can use in the controller part so let's let's go to this controller let's click on it and you reach to that product controller so here let's create a construct method so public function the function name construct so before that you have to add double underscore okay underscore underscore two times and then construct function and now guys inside this you can use your middleware so dollar this off middleware middleware function and and here you mention is admin okay and now guys you can get back refresh works fine so if you try to access without login let me just show you in incognito mode so here you see unauthorized that means it is not logged in and what in case of like it is logged in and the role is not given so let me just change the role here to user so here I have made it to user and let's get back refresh so here you see 43 forbidden which means you are not allowed to access I have not given the route here middleware middleware route in the web.php instead I have given in the controller okay so which will be applied to all the functions like your index create store update delete everything okay so now refresh so here you see and now guys if I just make it to admin admin refresh it works fine so guys we have successfully learned about the middleware in larl okay so like we have learned how to use the middleware in our routes that is web.php and also in our controller part here we can in the construct method and also the conditions in the middleware how to give and utilize it okay so guys in this video that's it thank you for watching this video please subscribe like and share
Info
Channel: Funda Of Web IT
Views: 1,841
Rating: undefined out of 5
Keywords: fundaofwebit, laravel middleware authentication, middleware in laravel in hindi, middleware laravel tutorial, laravel middleware hindi, create middleware in laravel, why we use middleware in laravel, how to use middleware in laravel, laravel middleware, laravel middleware auth guard, How to Use Laravel Middleware to Protect Your Application
Id: n_Ekd61aYoY
Channel Id: undefined
Length: 16min 28sec (988 seconds)
Published: Sun Jan 28 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.