Middlewares in Laravel

Dikshant Rajput
3 min readAug 14, 2021

--

In my previous blogs, I have told about the different aspects of authenticating and verifying users. Have a look on them here

In this blog, I will be telling about middlewares in Laravel that comes in very handy while dealing with tasks that we want to do before forwarding Http requests and after forwarding Http requests.

Middlewares are used for many tasks and you can use them to perform tasks like:

  • Authenticating users
  • Validating incoming requests
  • Change the default language
  • Checking roles
  • Checking permissions
  • Adding headers to http requests
  • Logging requests

Laravel itself uses a middleware that verifies the user of your application is authenticated or not. If the user is not authenticated, the middleware will redirect the user to your application’s login screen.

To create a middleware, use this command

php artisan make:middleware CheckAgeMiddleware

In this example, we will be checking the age of the user when he tries to access a particular route and will forbid his access if he/she is a minor.

For this purpose, we will be taking the dob of the user at the time of registration and calculate age in the middleware and provide access if the age is greater than 18. Let’s start…

Creating Middleware

After running the command, a CheckAgeMiddleware file will be created in app\Http\Middleware folder.

A middleware file generally consists of handle method with the following definition:

public function handle(Request $request, Closure $next){
//task
return $next($request);}

The $next($request) is used to proceed the request further. There are two ways of performing actions on the requests . One is before the http request and other way is after the http request.

The above method shows task to be done before the request have passed.

To perform task after the request, you can do something like this:

public function handle(Request $request, Closure $next){
$response = $next($request);
//task return $response;}

We have to just calculate the age of the user and restrict him from going further so we will use the first way like this:

public function handle(Request $request, Closure $next){    abort_if(Carbon::parse(auth()->user()->dob)->age < 18, 403 , 'Age Restricted Page');    return $next($request);}

The above handle method will first calculate the age and if it is less than 18, it will abort the request and show the forbidden(403) page with custom message.

Registering the middelware

We have to tell Laravel that this is the middleware and we have to use it so we have to register it in the Kernel.php file in the app\Http folder like this:

In the route middleware method, add this line'age.check' => CheckAgeMiddleware::class

Applying middleware on the route

In routes/web.php file, register the route and apply middleware on it like this:

Route::get('user/ageRestrictedPage',[App\Http\Controllers\UserController::class,'ageRestrictedPage'])->name('age.restricted.page')->middleware('age.check');

Now you can use this route anywhere and whenever a user will move on the route, application will check his/her age and if it is less than 18, it will show something like this:

This is how you can create your middleware and restrict users or log them or can perform any task.

All the references are taken from here:

You will find the code here:

I hope you like this and if you do, don’t forget to hit that clap icon and follow me for more such blogs.

--

--

Dikshant Rajput
Dikshant Rajput

No responses yet