DEV Community

loading...

How To Create Custom Middleware In Laravel

websolutionstuff profile image Websolutionstuff Originally published at websolutionstuff.com ・1 min read

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
Enter fullscreen mode Exit fullscreen mode

Read More : How To Create Custom Middleware In Laravel

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
Enter fullscreen mode Exit fullscreen mode

Read Also : CRUD Operation In PHP

Discussion (0)

pic
Editor guide