Middleware in Laravel

  1. Download and install the latest version of Composer from https://getcomposer.org/download/

  2. Open Windows Terminal in Visual Studio Code install Laravel Installer with command as below:

    composer global require laravel/installer
  3. Create new folder named LearnLaravelWithRealApps. Use Visual Studio Code open to LearnLaravelWithRealApps foler. In this folder, create new project named LearnLaravelWithRealApps with command as below:

    laravel new LearnLaravelWithRealApps

  4. Run LearnLaravelWithRealApps project with command as below:

    php artisan serve
  5. Open LearnLaravelWithRealApps project with url as below:

    http://localhost:8000

Create new PHP file named DemoController.php in app/Http/Controllers folder as below:

<?php

namespace App\Http\Controllers;

class DemoController extends Controller
{
    public function index()
    {
        return view('demo/index');
    }

    public function index2()
    {
        return view('demo/index2');
    }

    public function index3()
    {
        return view('demo/index3');
    }
}

Create new folder named demo in resources/views folder. In this folder, create new Blade file as below:

In resources/views/demo folder, create new Blade file named index.blade.php as below:

<html>

    <head>
        <title>Laravel</title>
    </head>

    <body>
        
        <h3>Index</h3>

    </body>

</html>

In resources/views/demo folder, create new Blade file named index2.blade.php as below:

<html>

    <head>
        <title>Laravel</title>
    </head>

    <body>

        <h3>Index 2</h3>

    </body>

</html>

In resources/views/demo folder, create new Blade file named index3.blade.php as below:

<html>

    <head>
        <title>Laravel</title>
    </head>

    <body>

        <h3>Index 3</h3>

    </body>

</html>

Use command line below to create new middlewares:

php artisan make:middleware YourMiddleware

Use php artisan make:middleware Log1Middleware command line to create Log1Middleware as below:

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;

class Log1Middleware
{    
    public function handle(Request $request, Closure $next)
    {
        echo '<br>date: '.date('d/m/Y H:i:s');
        return $next($request);
    }
}

Use php artisan make:middleware Log2Middleware command line to create Log2Middleware as below:

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\URL;

class Log2Middleware
{    
    public function handle(Request $request, Closure $next)
    {
        echo '<br>'.URL::current();
        return $next($request);
    }
}

Use php artisan make:middleware Log3Middleware command line to create Log3Middleware as below:

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;

class Log3Middleware
{    
    public function handle(Request $request, Closure $next)
    {
        echo '<br>ip: '.$request->ip();
        return $next($request);
    }
}

Use php artisan make:middleware Log4Middleware command line to create Log4Middleware as below:

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;

class Log4Middleware
{    
    public function handle(Request $request, Closure $next)
    {
        echo '<br>path: '.$request->path();
        return $next($request);
    }
}

Open App\Http\Kernel.php file and declare new middlewares to $routeMiddleware as below:

protected $routeMiddleware = [
    'log1Middleware' => \App\Http\Middleware\Log1Middleware::class,
    'log2Middleware' => \App\Http\Middleware\Log2Middleware::class,
    'log3Middleware' => \App\Http\Middleware\Log3Middleware::class,
    'log4Middleware' => \App\Http\Middleware\Log4Middleware::class,
];

Open web.php file in routes folder, add new routes as below:

<?php
				
use App\Http\Controllers\DemoController;
use Illuminate\Support\Facades\Route;

Route::group(['prefix' => 'demo', 'middleware' => ['log1Middleware', 'log2Middleware', 'log3Middleware']], function () {

    Route::get('/', [DemoController::class, 'index']);

    Route::get('/index', [DemoController::class, 'index'])->middleware(['log4Middleware']);

    Route::get('/index2', [DemoController::class, 'index2'])->withoutMiddleware(['log2Middleware', 'log3Middleware']);

    Route::group(['prefix' => 'index3', 'excluded_middleware' => ['log1Middleware', 'log2Middleware']], function () {
        Route::get('/', [DemoController::class, 'index3']);
    });
    
});

  • Access index action in Demo controller with http://localhost:8000/demo url:

  • Access index action in Demo controller with http://localhost:8000/demo/index url:

  • Access index2 action in Demo controller with http://localhost:8000/demo/index2 url:

  • Access index3 action in Demo controller with http://localhost:8000/demo/index3 url: