Middleware
What is Middleware

Middleware is a type filtering mechanism for HTTP requests. For example,The user of your application is check for user is authenticated or not by your controller contruct. if user authenticated will redirect of home page otherwise user can't login

Be Default In Laravel6.0 provide us some middleware that is available in app->Http->Middleware

Authenticate
CheckForMaintenanceMode
EncryptCookies
RedirectIfAuthenticated
TrimStrings
TrustProxies
VerifyCsrfToken

How to Create Middleware

php artisan make:middleware myuser

we can this middleware in app->Http->Middleware

namespace App\Http\Middleware;
use Closure;
class myuser
{
public function handle($request, Closure $next)
{
return $next($request);
}
}

Registering Middleware

For Registering Middleware we use app/Http/Kernel.php class. we can register middleware by two type.

Global Middleware
If you want a middleware to run during every HTTP request to your application, Then use $middleware property of Kernel.php class.

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

Assigning Middleware To Routes
If you want a middleware to specific routes, Then use $routeMiddleware property of Kernel.php class.Here we can list of register middleware same as we first assign the middleware a key then in front we write our middware path.

Sample route property
protected $routeMiddleware = [
'auth' => \App\Http\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class,
'can' => \Illuminate\Auth\Middleware\Authorize::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
];

How to use of Middleware?

Once the middleware has been defined in the Kernel.php, Then we can use middleware.

To assign middleware to the route
Route::get('admin/home', function () {
//
})->middleware('auth');

To assign multiple middleware to the route:

Route::get('/', function () {
//
})->middleware('firstmiddleware', 'secondmiddleware');

Middleware Groups

Sometimes we need to use of many middleware simultaneously. For this purpose we can group many middleware under a single key that is define in $middlewareGroups property of your kernel.php

protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],

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

Note : we use web key in route and and all middleware called automatically.


Previous