更改 Slim 请求 属性

Change Slim Request property

如何更改 Slim 请求的 URI 路径?我尝试了以下方法,但是,withPath() 克隆了 uri,因此请求对象没有改变。如果不可能,是否有一个简单的过程可以根据原始请求但新的 URI 路径创建新的 Slim 请求?

关于我为什么要这样做的背景。我有一个方法可以接受 Slim 请求,使用 Guzzle 向另一台服务器执行 cURL 请求,写入 Slim 响应,然后 returns Slim 响应。 API Guzzle 查询几乎相同的路径但带有版本号。

$app->get('/{basetype:guids|tester}/{guid}/logs/{id:[0-9]+}', function (Request $request, Response $response, $args) {
    switch($request->getQueryParam('ContentType')) {
        case 'text':
            //
            return $this->view->render($response, 'log.html', $rs);
        case 'file':
            $uri=$request->getUri();
            $path=$uri->getPath();
            $uri->withPath(_VER_.$path);
            return $this->serverBridge->proxy($request, $response, 'application/octet-stream');
    }
});

class ServerBridge
{
    protected $httpClient;

    public function __construct(\GuzzleHttp\Client $httpClient)
    {
        $this->httpClient=$httpClient;
    }

    public function proxy(\Slim\Http\Request $slimRequest, \Slim\Http\Response $slimResponse):\Slim\Http\Response {
        //$slimRequest is used to obtain data to send to Guzzle 
        $curlResponse = $this->httpClient->request($method, $path, $options);
        //use $curlResponse to get data and write to $slimResponse
        return $slimResponse;
    }
}

如果更改 Uri 路径,则也必须更改请求。这是设计使然。别担心,克隆在 PHP.

很便宜
<?php

$request = $request->withUri($request->getUri()->withPath('your/changed/path'));

Note: Be careful to use that new $request in some other calls in the middleware stack. They were made immutable for a reason. Changing the uri of a request early in the execution can have undesirable side effects.