How to use Middleware in Laravel 11 with Example For Beginners

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone this is iin and in this tutorial I'm going to talk about middle and how you can use middle in your L project so we use middle in L to protect a certain base for example we have two user on is regular user and another on is admin and we have a base where we only on the admin to have access so in that case we'll use Miner so using middleware we can stop regular user to get access to the endb base we can also use middle for like age as well if the user age is below 18 then we will not let that user have access to that started base we can also use mle like this as well so right now let me just show you how we can use midle in our L project so here uh you can see this is a lan project and we have login and register option over here so I have uh made this login and register option using jream and here I have a user table and in this user table we have two email address over here here you can see name email and user type and here you can see uh the user type we have user and admin user type so right now what we do is that we'll create a page in our LEL project and we'll only let the admin to have access to that page when they logged in as a admin so the user or have access to that BAS face okay when there loging as user so in order to do that first of all let's go and create a page first so let me just go to my code and we'll go to Resource then views and inside views let's say we just create a blade file over here in this view so we'll just right click on The View then click new file and let's say the file name will be admin page. blade. PHP control s then said so here we'll just write simple HTML and in the body we are going to say only admin is allow to this page so right now let's just save this and we're going to create it out for this page so let's go to our routes then web. PHP and here let me go down and let's say the route will be K and the route name will be let's say admin page we'll give a comma we'll give to T bracket and let's go and mention the bo controller name we'll mention the class name and to sing cotation let's say the function name will be page so let's save this and right now let's go to our home controller and create this page so for it we'll go to app HTTP controller and then here you can see home controller so from here we just return the simple view so here we're going to send public function P to first bracket then to Second bracket so we just sell on here return View and our view is uh this one you can see admin base. blade. PHP you can see this one so we are just going to S here admin page so right now if we just save this and there you can see our you relas admin page right so we'll just go to our browser and if we just write over here slash admin p and press enter there you can see only admin is allowed to this space so we are able to see this because we haven't added any middle in our LEL project so right now let's go and create a middle for our LEL project so in order to create middle we need to go to our common prompt which is in the project directory and we're going to S here in order to get the middle you need to s had PSP Artisan main clone middle or and we need to give our middle name and the middle name we normally give about gu so let's say the middle name will be admin then if we now press enter there you can see a middle L is created so where is the file located if we just go to add HB middle L there you will find admin. P so let's go to our code and then here if we go to app then HTTP inside HTTP there you can see we have a middleware folder and in Middle we have admin. PHP so here first of all what we said is that if the user is logged in and the user type value is user or admin so over here in admin. BC we need to check that so in order to check if the user is logged in we need over here use illuminate back slash support back slash packets back slash it will be all so right now using this we'll be able to check whether a user is loed so right now in this public function handle we need to write a condition so that condition will be we need to S here if condition we s here if then we mention or double CL on user here you can see user this user Table Na and we are using o over here you have to check if a user is loog in or not then we'll give this Arrow sign which over here user type is equal equal is it admin if the user type is admin then we are going to say okay the request is f and if the user type is not admin we are going to S here award award 40 or it means it will show us a message that unauthorize access and next request it means okay the request is fine go ahead if the user type is ending and we need to do one more thing and that is we need to register this admin. PHP so in order to do that we need to go to our here we can see boep folder and in the boep folder we have AB do PHP so here you can see Middle with middle function middle Dollar middle here we need to write two line of code in order to add this admin middle so here we're going to say Dollar middle or this Arrow sign alas to first bracket to third bracket like this and after it let me just write it like this and here we're going to measure and variable you can name it anything we can say here admin or admin type anything that's fine so we'll go with admin then this equal is geter than sign then we'll give this back slash then we saw Here app back slash HTTP back slash midle or back slash admin we give them double dra with here class then ended with a comma so here we are just basically mentioning the air HTTP midle wi and then this admin. phsp over here okay so we can just save this it means we created our admin middle so right now what we need to do is that we need to add this middle o with our admin base here you can see route get admin base so for this we're going to add this middle and in order to add that middle L what we need to do is that we need to give this Arrow sign then we need to over here middle or to first braet to single quotation and here you can see we write over here admin equal to admin equal to this middle so we need to write this admin or whatever your variable is over here to over here so let's set this so here in the admin. PHP we mention over here if the user type is admin only a certain request will be accepted and we mention over here the middle admin for admin P so right now let me just refresh around here then let me just log in as a anding first so right now we'll give our admin username about here then we'll give the admin password about here then click login and then you can see currently we are in the admin home and we have the log out option so right now here you can see from the admin the user type is AD me so right now we should have access to our admin P right so let me just copy this and if we just over here SL home if we just over here SL admin p and press enter there you can see only admin is allowed to dis space so the login user had access to this space so right now let me just log out from here and let me just log in as a user and let's see if a user can have access to that P when they are logged in as a user so right now our here we'll give the user email then the user password Here click login and there you can see currently a user is logged in and over here you can see for user the user type is user so right now since a user is logged in so right now if we just try to go to the admin page it should uh show us this abroad 41 message it means unauthorized because the user type is admin over here so right now if we just go back then if we just say over here slash admin page and then press enter there you can see it showing as a message for on unauthorized so there you can see unauthorized access so each of this unauthorized access you can also so send the user to a specific view for example if you just over here return redirect and if you want to send the user to the home page so we just hand the slash it means it will uh send the user to the root view so right now if we just save it and if a user is logged in over here and if we now try to go to the admin base it will send us to the root View so right now let me just press what here enter there you can see to go to the room F so right now let me just go back and uh and from here I'll just log out and we'll check if a log out user can have access to the and base so there you can see currently I'm logged out and if I just over here slash admin paste then press enter it will give us a eror let me just show you there you can see attempt to read property user type on null so the thing is uh let me just go back to the admin.php so here you can see all user type and here we mention our mdle admin so the thing is whenever we trying to go to the admin page it will check the login user user type but since no user is logged in so it will give us a error so in order to solve that issue we just need to say over here okay let me just remove this from here like this we just need to give to turn here then to sing cation will it's over here or then comma to sing cation then and like this after that what it will do is that whenever we are calling this URL it will check if there is any loged in user and if there is no loog in user it will send the user to the login page so right now if I now uh let me just go back over here and if I just s on here slash admin past then casess enter Then you can see it send us to the login page so this how you can use mider in your LEL project and if you want to know how to create multiple authentication in LEL J or LEL PR then you can check out the video on the screen thank you very much for watching make sure to subscribe and I hope to see you in the future tutorials
Info
Channel: Web Tech Knowledge
Views: 3,186
Rating: undefined out of 5
Keywords: laravel middleware, middleware in laravel, middleware laravel, laravel middleware authentication, laravel middleware tutorial, what is laravel middleware, route middleware in laravel, what is laravel framework in php, how to use middleware in laravel, what is middleware, middleware tutorial for beginners, why and how to use middleware in laravel, why use middleware in laravel, why middleware is used, custom middleware, laravel custom middleware, laravel middleware auth
Id: Gr1Mmb1KYA8
Channel Id: undefined
Length: 12min 59sec (779 seconds)
Published: Sat Mar 23 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.