You are here

public function RequestTrait::withUri in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 vendor/zendframework/zend-diactoros/src/RequestTrait.php \Zend\Diactoros\RequestTrait::withUri()

Returns an instance with the provided URI.

This method will update the Host header of the returned request by default if the URI contains a host component. If the URI does not contain a host component, any pre-existing Host header will be carried over to the returned request.

You can opt-in to preserving the original state of the Host header by setting `$preserveHost` to `true`. When `$preserveHost` is set to `true`, the returned request will not update the Host header of the returned message -- even if the message contains no Host header. This means that a call to `getHeader('Host')` on the original request MUST equal the return value of a call to `getHeader('Host')` on the returned request.

This method MUST be implemented in such a way as to retain the immutability of the message, and MUST return an instance that has the new UriInterface instance.

@link http://tools.ietf.org/html/rfc3986#section-4.3

Parameters

UriInterface $uri New request URI to use.:

bool $preserveHost Preserve the original state of the Host header.:

Return value

static

File

vendor/zendframework/zend-diactoros/src/RequestTrait.php, line 233

Class

RequestTrait
Trait with common request behaviors.

Namespace

Zend\Diactoros

Code

public function withUri(UriInterface $uri, $preserveHost = false) {
  $new = clone $this;
  $new->uri = $uri;
  if ($preserveHost && $this
    ->hasHeader('Host')) {
    return $new;
  }
  if (!$uri
    ->getHost()) {
    return $new;
  }
  $host = $uri
    ->getHost();
  if ($uri
    ->getPort()) {
    $host .= ':' . $uri
      ->getPort();
  }
  $new->headerNames['host'] = 'Host';
  $new->headers['Host'] = [
    $host,
  ];
  return $new;
}