Module

Middleware

Introduction

Middleware provide a convenient mechanism for inspecting and filtering HTTP requests entering your application. It is very essential for the security of the application. For example. You want your users to see the particular page of website only if user is authenticated. Then with the help of middleware you can check that particular user is authenticated or not, if the user is not authenticated then middleware will redirect the user to your application's login screen. However, if the user is authenticated, the middleware will allow the request to proceed further into the application.


Usage

Step-1

Use the below command to create middleware in VaahCMS

npx vaah cms:m-make middleware <module-name> <middleware-name>

For more commands click here e.g

npx vaah cms:m-make middleware Travel Auth

This command will create a Auth middleware in VaahCms/Modules/Travel/Http/Middleware/ directory

Follow below image for reference:

Step-2

After creating the middleware, now you need to register it by using registerMiddleware function.
VaahCms/Modules/<module-name><Providers>/<module-name>ServiceProvider.php

e.g VaahCms/Modules/Travel/Providers/TravelServiceProvider.php

Follow below image for reference:

Step-3

Since you have registered it, now you can use this middleware in route file.
Follow below image for reference:


Copyright © 2024