Laravel 8 Tutorial - Middleware
In this video, we are going to learn about middleware in Laravel 8. Middleware provides a convenient mechanism for filtering HTTP requests entering your application.
All middleware is located in the Middleware directory. Let's see how we can create a middleware. For creating a new middleware, use the make:middleware Artisan command.
So, switch to the command prompt and run the command:
php artisan make:middleware CheckUser
Here, CheckUser is the middleware name. Now, go to the app folder, then HTTP, then middleware. Alright, now you can see the CheckUser middleware.
Now, open this file and inside the handle() method, just write:
echo "CheckUser midddleware is applied on this route";
Now, we need to add this middleware to the application. Middleware can be used in three ways: at the global level, at the grouped level, and at the route level.
When we use middleware globally, it will be applied to the whole application. So, let's use middleware as globally first. For that, go to the app, then HTTP.
Just open the kernel.php file. Inside the middleware array, add the CheckUser middleware class:
\App\Http\Middleware\TrustProxies::class,
Make sure to add a comma at the end of the line. Save this file. Now, switch to the browser and refresh the page. You can see the 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 the /login route also. It means that when we use middleware as globally, it is applied to all routes.
Now, let's see the middleware group. The middleware group is applied to particular routes groups. It means that when we add middleware 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 to web.php related routes. Alright, let's see how we can use this. So, switch to the kernel.php file and first remove this line from here:
\App\Http\Middleware\CheckUser::class,
Go to the middleware group array and inside this array, write the following code:
\App\Http\Middleware\CheckUser::class,
Now, save the file and let's check this. Switch to the browser and refresh the page. Now, you can see the message. Now, check on other routes. It's working on this route also.
Alright, now check on API group routes. For that, just open the api.php file and let's check this route. So, in the browser, just type here /users/smith. Press enter. You can see that the middleware is not working here.
Because we added the middleware to the web group, not the API group. For applying middleware to the API group, just go to the kernel.php file and remove the line from the middleware group array.
Now, add the following code inside the API group:
\App\Http\Middleware\CheckUser::class,
Now, let's check. So, switch to the browser and refresh the page. You can see the middleware working on API routes. And when we go to the web routes, the middleware is not working here.
Alright, now let's see the route middleware. Go to the kernel.php file and remove the line from here. Scroll the page and go to the route middleware and write the following code:
'checkuser' => \App\Http\Middleware\CheckUser::class,
Now, go to the web.php file and here, with the route, add the middleware as follows:
Route::get('/login',[LoginController::class,'index')->middleware('checkuser');
Now, let's check this. So, switch to the browser and go to the URL /login. Now, you can see the middleware working on this users route. When we check with a different route, you can see that the middleware is not applied.
Ok, so in this way, you can use middleware in Laravel.