Laravel12のミドルウェアの検証

# はじめに
本記事はLaravel12でのミドルウェアの動きを検証するために行なった。

前回の環境構築から続けて作業を行う。
https://qiita.com/pig_buhi555/items/530b1d6d44ec3de62890

以下のURLにアクセスした時、ログ出力しミドルウェアの動きの検証を行う。
http://localhost:8000/


# 1. ソース
```laravel12/bootstrap/app.php
<?php

use Illuminate\Foundation\Application;
use Illuminate\Foundation\Configuration\Exceptions;
use Illuminate\Foundation\Configuration\Middleware;
use App\Http\Middleware\AppendMiddleware;
use App\Http\Middleware\CustomEmptyStringsToNull;
use App\Http\Middleware\PrependMiddleware;
use Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull;

return Application::configure(basePath: dirname(__DIR__))
    ->withRouting(
        web: __DIR__.'/../routes/web.php',
        commands: __DIR__.'/../routes/console.php',
        health: '/up',
    )
    ->withMiddleware(function (Middleware $middleware) {
        /*** ここに使用するMiddleware記載 ***/
        /*** php artisan make:middleware ミドルウェア名 で作成 ***/
        // 最初に追加
        $middleware->prepend(PrependMiddleware::class);
        
        // 最後に追加
        $middleware->append(AppendMiddleware::class);
        // デフォルトのミドルウェアの書き換え
        $middleware->replace(ConvertEmptyStringsToNull::class, CustomEmptyStringsToNull::class);

        // ミドルウェア削除(例. PrependMiddleware)
        $middleware->remove(PrependMiddleware::class);
        // PrependMiddlewareは実行されない
        $middleware->prepend(PrependMiddleware::class);
    })
    ->withExceptions(function (Exceptions $exceptions) {
        //
    })->create();
```

```laravel12/app/Http/Middleware/AppendMiddleware.php
<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;

class AppendMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response)  $next
     */
    public function handle(Request $request, Closure $next): Response
    {
        logger('AppendMiddleware');
        return $next($request);
    }
}
```

```laravel12/app/Http/Middleware/CustomEmptyStringsToNull.php
<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;

class CustomEmptyStringsToNull
{
    /**
     * Handle an incoming request.
     *
     * @param  \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response)  $next
     */
    public function handle(Request $request, Closure $next): Response
    {
        logger('CustomEmptyStringsToNull');
        return $next($request);
    }
}
```

```laravel12/app/Http/Middleware/ReplaceMiddleware.php
<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;

class ReplaceMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response)  $next
     */
    public function handle(Request $request, Closure $next): Response
    {
        logger('ReplaceMiddleware');
        return $next($request);
    }
}
```

# 2. URLにアクセス
http://localhost:8000/

# 3. ログ
```laravel12/storage/logs/laravel.log
[2025-05-24 07:25:40] local.DEBUG: CustomEmptyStringsToNull  
[2025-05-24 07:25:40] local.DEBUG: AppendMiddleware  
```

# 4. 参考サイト
ありがとうございました。
https://zenn.dev/naopusyu/articles/86222755fb7168