基于webman,如何设计request_id呢?

yysu

请教,基于webman,如何设计request_id呢?

252 1 1
1个回答

luojianqun

在config/middleware.php配置全局中间件
return [
// 全局中间件
'' => [
app\middleware\RequestId::class,
]
];

app\middleware目录新建RequestId.php

namespace app\middleware;

use Webman\MiddlewareInterface;
use Webman\Http\Response;
use Webman\Http\Request;

/**

  • Class StaticFile
  • @package app\middleware
    */
    class RequestId implements MiddlewareInterface
    {
    public function process(Request $request, callable $next): Response
    {
    $requestID = strtoupper(uniqid(date('ymdHis') . create_random_string(2), true));
    $request->requestID = $requestID; // 带出
    $response = $next($request);
    return $response;
    }
    }
🔝