Hi all,
trying to make php-proxy do something a bit different in that I would like to be able to change both the target (no problems there) and the path (not working so far)
It appears that the request object has the Uri as a private object and I don’t seem to be able to modify it… is this correct?
Thanks
Hello and welcome to our forums!
You can change the URI. Here a short example of the usage:
$request = Laminas\Diactoros\ServerRequestFactory::fromGlobals();
var_dump((string) $request->getUri()); // https://getlaminas.org
$request = $request->withUri(new Laminas\Diactoros\Uri('http://localhost'));
var_dump((string) $request->getUri()); // http://localhost
I hope it helps to find a solution to your problem.
Thanks for that
This code did what I was looking for:
//to change the host and replace '/api/db' from the path
$request = ServerRequestFactory::fromGlobals();
$uri = $request->getUri();
$uri = $uri->withHost('192.168.1.161:5984');
$path = $uri->__toString();
$pattern = '/\/api\/db(.*?)\?/';
preg_match($pattern,$path,$match);
$path = $match[1];
$uri = $uri->withPath($path);
// show fixed uri
print_r($uri->__toString());
//push new uri into request - this is inside a class thus '$this->' also needed a leading '\' on the Laminas class
$this->request = $this->request->withUri(new \Laminas\Diactoros\Uri($uri));
Here is the reference to the related PSR standard (PSR-7) with the explanation for RequestInterface::withUri()
: