How to use middleware in Laravel?

A Middleware in Laravel is a processing unit between Request and Response. It can be used for filtering of requests, It is also used for stepwise processing of requests.

All middleware are stored in the app/Http/Middleware directory.

Middleware is created by the  make:middleware artisan command.

Let us make a simple middleware that will differentiate between odd and even numbers.

php artisan make:middleware CheckOddEven

This creates the following file automatically.


<?php

namespace App\Http\Middleware;

use Closure;

class CheckOddEven
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
return $next($request);
}
}

&amp;nbsp;

We will change the middleware in this manner


<?php

namespace App\Http\Middleware;

use Closure;

class CheckOddEven
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
echo "Odd Even Middleware";
return $next($request);
}
}

&nbsp;

The routes in web.php


Route::get('/oddeven', function () {
return view("oddeven",['result'=>'']);
});

Route::post('/oddeven', 'OddEvenController')->middleware('oddeven');

&nbsp;

The output is at the top.

Next, we create a Global Middleware. Here is the code.


<?php

namespace App\Http\Middleware;

use Closure;

class NewGlobalMiddleware
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
echo "I am global middleware";
return $next($request);
}
}

Registration of the Middleware.


&nbsp;

protected $middleware = [
\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
\Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
\App\Http\Middleware\TrimStrings::class,
\Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
\App\Http\Middleware\NewGlobalMiddleware::class,
];

The complete Kernel.php file


&nbsp;

<?php

namespace App\Http;

use Illuminate\Foundation\Http\Kernel as HttpKernel;

class Kernel extends HttpKernel
{
/**
* The application's global HTTP middleware stack.
*
* These middleware are run during every request to your application.
*
* @var array
*/
protected $middleware = [
\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
\Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
\App\Http\Middleware\TrimStrings::class,
\Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
\App\Http\Middleware\NewGlobalMiddleware::class,
];

/**
* The application's route middleware groups.
*
* @var array
*/
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
// \Illuminate\Session\Middleware\AuthenticateSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],

'api' => [
'throttle:60,1',
'bindings',
],
];

/**
* The application's route middleware.
*
* These middleware may be assigned to groups or used individually.
*
* @var array
*/
protected $routeMiddleware = [
'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
'can' => \Illuminate\Auth\Middleware\Authorize::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'oddeven' => \App\Http\Middleware\CheckOddEven::class,
];
}

Leave a Reply