What is Middleware ? How to use Middleware in Laravel with Example

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello Beyond this is Yamin and in this tutorial we are going to talk about media Lord in laravel so first of all we'll talk about what is medulla and why we use middleware and after that we'll talk about how we can use middle or int larabelle so first of all what is middleware and why we need to use middleware now let's say we have a base in our website and we only on the admin to have access to that base then we need to use middleware middleware will allow us to make some conditions so that users will not have access to that certain admin page we cannot not only use mineraler for like admin access we can also mirror for other condition as well let's say if a user is not over 18 years old then will not let that user to access to a certain base then at that place we can also use middle hours so let me just show you with the example how we can use middleware so here you can see I have created a fresh laravel project over here so first of all in this let me just add brace authentication over here so I'll just go to the go to the common prompt and over here I am going to write composer require laravel slash please so it will install brace in our laravel projects so as you can see it is installing and the installation is complete so after this command we saw here PHP Artisan priest clone install then press enter and here I'll choose blade we'll say 0 over here with over here dark Port no and no so it will install Breeze in our project so using the breeze example I am going to show you how you can like allow only the admin have access to a certain page so in that certain page the user won't be able to have access so after this I am just going to server here in PM install and right now we'll show it here npm run build okay so npim Brand Build is also done so right now if we just refresh our browser over here you can see up login and register option over here and this average Sharpies sorry so right now one more thing we'll do is that we'll just go to our project over here and we'll just actually go to the database migration and inside migration we have the user table so in this user table we will take one more extra field over here so let me just copy this and I'm going to paste this over here and here we are going to say user type user type and let's over here user type will be null above okay so right now we save this then we'll just run the command PS condition migrates so let's say over here PHP are tison migrate press enter and the table is migrated so right now if we just go to our database table then let's refresh here and this out table database and here you can see the user table so we have name email and user type over here so let's just register the user let's over here test then we saw over here test at the rate of gmail.com let's give a password then concom password click register okay it took us to the dashboard so right now if we just refresh over here here you get that used user type is null over here so right now let's go and create a paste and we'll only let the user type admin to have access to that certain page so let's go to our code again and right now let's go to our resource views and inside views let's just create a view over here let's say it will be admin page dot LED dot PHP they'll save it Ctrl s and then save so in this admin test let's just over here normal HTML and inside the body we are going to say h on only admin have access to this page so right now we'll go to our routes and create a URL so that we can return A View to this admin page dot plate.php so let's go to our routes then web.php and I already added the home controller at the top so here I'm just going to say raw wood double clone gate and here we'll say let's say slash it will be admin page then we'll come over here to third bracket and then we are going to say here home controller Tower clone class comma let's say the function name will be index okay so let me just make this a little bit smaller so right now let's go to our home controller and create this function index so we'll go to app HTTP controller and home controller so right now here we are going to say public function index 25 bracket 10 to Second bracket and from here we'll just return a view to the this page admin page.blade.php so we'll cover here admin page so right now let's save this and save over here and here so right now if we just write this in the browser it will return us this view uh this view okay so let me just refresh over here and if we just write over here admin page then you know press enter here you can see we are currently in the admin page only admin have access to this page now the thing is we only on the admin to have access to this space and clearly this user is not admin here you can see user type is now so for it what we need to do is that we need to use midi lawyer so how we can use middleware and how we can first create a meteor so in order to create a meter oil we need to write this command and that is PHP RT sun make clone middle w a r e middleware and then we need to give our middleware name less sound middle and name will be admin and normally we need to start it with capital okay so then if we press enter here you can see middleware is created and where is the middleware so right now if we just go to our code code and then here if we just go to the let me just show you if you just go to app that HTTP and inside HTTP you'll see the mirror folder over here and inside middleware here you can see we have admin.ph speed meet alert created over here so first of all uh what you need to do is that we need to register this admin dot PHP middleware in this kernel.php over here so where is the kernel.php here you can see if you just go inside the HTTP here you can see kernel.php so we'll just directly go to kernel.php and we will just directly go down and over here you can see mirror early accesses so here we are going to register our admin middleware so first of all here we are going to give to single quotation and let's over here add mean then we'll see over here equal and here you can see admin this is just a variable you can basically name this anything so don't worry and after this we need to give this backslash over here then we need to server Here app again backslash then with our here HTTP backslash it will be middleware backslash and we'll say over here admin double clone class then we'll give a comma over here so this is how basically we need to write to the register our admin middleware over here okay so while this admin middleware this one so once you just write this it means our middlewise register so you can just close the kernel.php so right now we need to go to our media lawyer and inside middleware we have admin.php so inside this admin dot PHP we need to write our condition over here inside this public function handle okay so here we give if condition and here first of all we will check if the user type is admin if the user type is admin only then will let the user have access to this page so for it what we need to do is that we'll say over here first if auth first rocket then this Arrow sign and we will mention the table name which is user so with our here user to files for arcade this Arrow sign after this will set the table column name which is user type then we'll give double equal and let's say the value for the user type will be adding so when the value for the user type is admin only then we let the user to have access to that certain page okay so we will just cut this return next to over here it means if the user type is admin you are okay to go to that base else will just over here a b o r d abroad we'll just show a message that for o on it means you are unauthorized so right now let's save this so how we can apply this for mq so for it we need to go to our web.php and here you can see we have the view admin page so here all you need to do is that at the end of this route will give this Arrow sign and we will show over here middle wire to five bracket to single quotation and with over here and pin so where is this admin coming from and here uh if we just go to kernel.psp here you already saw over here that I write over here add me this admin is basically this one so whenever someone calls this middleware admin then it means we are basically pointing this on and our this admin is Will point admin which is this one and then it will eventually come to this and check the user type and decide whether let the user have access to that certain page or not so right now currently uh obviously the user type is now so right now here if we now refresh over here okay it is giving us either in the kernel.php line 67 expecting okay let me just go back let's go to our kernel.php and here uh we need to give this Arrow sign here so we'll just say this okay so I made a mistake over here so right now let's set this again and if we now refresh here you can see it is showing us a message that unauthorized for o on quiz we just write over here so right now let's go and make the user type admin so let's go to our database table and here let me just change the user type to admin and here you can see right now the user type is admin and here if we now try to access to this admin paste and refresh over here here you can see we can have the access to this page so right now I would like to show you one more thing and that is if we just go back and here if you just log out from here and here if you just server here slash admin page and press enter it will give us a error and that is attempt to date property user type on null the reason is here you can see that whenever we are trying to go this URL it is calling the Midler admin and in the admin middleware we are mentioning the auth over here auth user table so basically it cannot find the auth user table that's why it is giving us the error so what we can do is that for the solution all we need to do is that we just need to give two third bracket over here and here and then before this we will give to single quotation with over here auth like this okay so it means it will check the auth first and if there is no logged in user then it will send the user to the login dashboard okay so right now let's save this again and let's go back let's refresh here and if we now try to access to the admin page and it is sending us to the login page so right now let's log in from here let's test at that of gmail.com let's give the password click login so it took us to the user dashboard so since the user type is admin so we obviously can have access to the admin page if we see over here admin page we have the access and if we just remove the user type admin to let's say user or anything else here you can see the types user so right now if I now refresh here you can see it is returning us a unauthorized view so this is all about the structural I hope you are clear about a concept of how you can use middleware basically using meteor you can stop the user from having access to certain page or certain data so basically this is all I hope this detail was useful to you if you want more detail like this make sure to subscribe I really appreciate that as well thanks you for watching I hope to see you in the future tutorials
Info
Channel: Web Tech Knowledge
Views: 7,099
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: DYTJFqALX7o
Channel Id: undefined
Length: 13min 46sec (826 seconds)
Published: Mon May 29 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.