How To Create Custom Middleware In Laravel
In this tutorial I will give you information about middleware and I will show you how to create custom middleware in laravel.
Laravel includes a middleware that verifies the user of your application is authenticated. If the user is not authenticated, the middleware will redirect the user to the login screen.If in your project’s have multiple user then we need to use middleware to provide diffrent access or login to diffrent users.
So, let’s start and follow my step and create your own custom middleware.
Step 1 : Create Middleware
Step 2 : Register This Middleware on Kernel File
Step 3 : Add Route
Step 4 : Add Controller
In this example, I have created “roleType” middleware and I will use simply on route, when they route will run you must have to pass “type” parameter and then you can access those request like as below demo link:
http://localhost:8000/check/role?type=user
http://localhost:8000/check/role?type=admin