Middleware Create in Laravel

In laravel, there is a very important work of middleware in middleware in laravel that the url that the user has called is placed inside the middleware, so that url can not call without the user login. This is done as a user authentication.

Keywords :- Middleware Create in Laravel 6.0, Middleware Create in Laravel 5.8, Middleware Create in Laravel 5.7, Laravel 5.6, Laravel 5.5, Laravel 5.4, Laravel 5.3, Laravel 5.2

Command to Create Middleware in Laravel

php artisan make:middleware <middleware-name>

Example: –

Step1: – Now we make a middleware named AgeMiddleware. To make this, we need to run the following commands –

php artisan make: middleware agemiddleware

Step2: –  After the command-completion, the following output will be received, you can see it in this image.

Step3:- After your command runs, the file named under the App/Http/Middleware will be created.

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

Step4:- After this, to protect this middleware, the middleware has to be protected in the file of the app/Http/Kernel.php

namespace App\Http;
use Illuminate\Foundation\Http\Kernel as HttpKernel;
class Kernel extends HttpKernel {
   protected $middleware = [
   protected $routeMiddleware = [
      'auth' => \App\Http\Middleware\Authenticate::class,
      'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
      'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
      'Age' => \App\Http\Middleware\AgeMiddleware::class,

Step5:- Now we create meddleware in the path file, any url you keep inside the middleware will not open until the user login.

Route::group(['middleware' => 'AgeAdminAuth'],function(){
Route::get('userlogin', controllername@userlogin);