首页 > 解决方案 > 这个表达式(终止)用于什么目的?

问题描述

我的终止中间件如下:

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Log;

    class ExampleMiddleware
    {
        /**
         * Handle an incoming request.
         *
         * @param  \Illuminate\Http\Request  $request
         * @param  \Closure  $next
         * @return mixed
         */
        public function handle($request, Closure $next, $role)
        {
            if ($role == null) {
                return redirect('/deneme');
            }
            return $next($request);
        }
        public function terminate($request, $response)
        {
           sleep(4);//what is the purpose in this section(What this means is that anyone who has used this way?)
        }
    }

终止函数的目的是什么,它是什么以及何时应该使用它?

标签: laravel

解决方案


有时,在准备好 HTTP 响应之后,中间件可能需要做一些工作。例如,Laravel 中包含的“会话”中间件在响应完全准备好后将会话数据写入存储。如果您在中间件上定义了终止方法,它将在响应准备好发送到浏览器后自动调用。

可终止的中间件


推荐阅读