阅读(1939) (8)

Laravel 8 前置 & 后置 中间件

2021-06-24 09:21:00 更新

中间件是在请求之前或之后执行,取决于中间件本身。例如,下面的中间件将在应用处理请求之前执行一些任务:

<?php

namespace App\Http\Middleware;

use Closure;

class BeforeMiddleware
{
    public function handle($request, Closure $next)
    {
        // Perform action

        return $next($request);
    }
}

然而,下面中间件是在应用请求之后执行一些任务:

<?php

namespace App\Http\Middleware;

use Closure;

class AfterMiddleware
{
    public function handle($request, Closure $next)
    {
        $response = $next($request);

        // Perform action

        return $response;
    }
}