In this video we are going to learn about the middleware.
Middleware provides a convenient mechanism for filtering HTTP requests, to entering in your application.
All middleware's are located in the Middleware directory.
Let's see how can we create a middleware.
For creating a new middleware, use make:middleware Artisan command.
so switch to command prompt and run the command.

php artisan make:middleware CheckUser

Here CheckUser is midddleware name.
Now go to the app folder then http then middleware.
Alright now you can see here CheckUser middleware.
Now open this file and and inside the handle() just write here here.

echo \"CheckUser midddleware is applied on this route\";

Now we need to add this middleware to the application.
Middleware can be used in three way
at global level, at grouped level and at route level.
When we use middleware globally.
It will be applied on whole application.
So, lets use middleware as globally first.
For that goto the app then http.
Just open kernel.php file.
Inside the middleware array add the checkuser middleware class.


Make sure comma must be added at end of line ok save this file.
Now switch to browser.
Refresh the page.
you can see here middleware message;
that was written earlier inside the middleware handler method.
Alright now change the route.
So just add /login and you can see it still working on /login route also.
It means when we use middleware as globally,
It applied on all the routes now lets see middleware group.
Middleware group applied on particular routes group.
It means when we add middle with api.php.
Then it will work only on api.php related routes.
When we add middleware with web.php, then it will be applied on web.php relateds routes.
Alright, let see how can we use this.
So switch to the kernel.php file and first remove this line from here


Go to the middleware group array and inside this array write the following code.


Now save the file and lets check this.
Switch to the browser and refresh the page.
Now you can see here the message.
Now check on other routes.
Its working on this route also.
Alright now check on api group routes.
For that Just open api.php file.
and lets check this route.
So in browser just type here.
Press enter You can see here middleware not working here.
Because we added the middle on web group not on api group.
For applying middleware on api group just go to the kernel.php file.
and just remove \\App\\Http\\Middleware\\CheckUser::class, from midddleware group array.
Now add the following code inside the api group.


Now lets check so switch to the browser and refresh the page.
You can see middleware working on api routes.
and when we go to the web routes.
middleware is not working here.
Alright now lets see the route middleware.
Goto the kernal.php file and remove \\App\\Http\\Middleware\\CheckUser::class, line from here.
Scroll the page and goto the route middleware and write the following code.

'checkuser' => \\App\\Http\\Middleware\\CheckUser::class,

Now goto the web.php file and here with the route add the midddleware as following.


Now lets check this, so switch to browser go to the url /login.
Now you can see middleware working on this users route.
When we check with different route.
You can see here middleware is not applied.
Ok so in this way you can use middleware in laravel.