withHeader('Access-Control-Allow-Origin', '*') ->withHeader('Access-Control-Allow-Credentials', 'true') ->withHeader('Access-Control-Allow-Headers', '*') // Headers 可以根据实际情况进行改写。 ->withHeader('Access-Control-Request-Headers', '*') ->withHeader('Access-Control-Allow-Methods', '*') ->withHeader('Access-Control-Max-Age', 1800); Context::set(ResponseInterface::class, $response); if ($request->getMethod() == 'OPTIONS') { return $response; } return $handler->handle($request); } }