Skip to content

RequestUrl

Immutable holder for the URL metadata WebRequest exposes alongside the wrapped PSR-7 request: scheme, host, port, path, query, and the derived request URI / full URL / protocol string.

final class RequestUrl

SourceRequest/RequestUrl.php
PropertyTypeDescription
$hoststringreadonly.
$pathstringreadonly.
$portintreadonly.
$protocol?``stringreadonly.
$querystringreadonly.
$requestUristringreadonly.
$schemestringreadonly.
$urlstringreadonly.

public function __construct(?string $protocol = null, string $scheme = '', string $host = '', int $port = 0, string $path = '', string $query = '', string $requestUri = '', string $url = ''): mixed

ParameterTypeDescription
$protocol?``string
$schemestring
$hoststring
$portint
$pathstring
$querystring
$requestUristring
$urlstring

Returns mixed

MethodDescription
authority(bool $forcePort = false): stringReturns the authority as host[:port].
effectivePort(): intEffective port: falls back to the scheme default (443/80) when unset.
fromServerParams(array<string, mixed> $server): RequestUrlDerive legacy URL metadata from PHP’s server parameters when no PSR-7 request is available (e.g.
fromUri(UriInterface $uri, array<string, mixed> $serverParams, string $protocolVersion): RequestUrlDerive URL metadata from a wrapped PSR-7 request’s URI.
fullUrl(): stringReturns the absolute URL, recomputed from the current scheme, authority and request URI rather than from the stored url property.
isHttps(): boolReports whether the scheme is exactly https.
withHost(string $host): RequestUrlReturns a copy using the given host.
withPath(string $path): RequestUrlReturns a copy using the given path.
withPort(int $port): RequestUrlReturns a copy using the given port.
withProtocol(?string $protocol): RequestUrlReturns a copy using the given protocol string, e.g.
withQuery(string $query): RequestUrlReturns a copy using the given query string, without the leading ?.
withRequestUri(string $requestUri): RequestUrlReturns a copy whose request URI is the given string.
withScheme(string $scheme): RequestUrlReturns a copy using the given scheme.

public function authority(bool $forcePort = false): string

Returns the authority as host[:port].

The port is appended only when it is not the scheme’s default, unless $forcePort asks for it unconditionally.

ParameterTypeDescription
$forcePortbool

Returns string

public function effectivePort(): int

Effective port: falls back to the scheme default (443/80) when unset.

Returns int

public static function fromServerParams(array<string, mixed> $server): RequestUrl

Derive legacy URL metadata from PHP’s server parameters when no PSR-7 request is available (e.g.

ParameterTypeDescription
$serverarray``<``string``, ``mixed``>

Returns RequestUrl

public static function fromUri(UriInterface $uri, array<string, mixed> $serverParams, string $protocolVersion): RequestUrl

Derive URL metadata from a wrapped PSR-7 request’s URI.

ParameterTypeDescription
$uriUriInterface
$serverParamsarray``<``string``, ``mixed``>
$protocolVersionstring

Returns RequestUrl

public function fullUrl(): string

Returns the absolute URL, recomputed from the current scheme, authority and request URI rather than from the stored url property.

Returns string

public function isHttps(): bool

Reports whether the scheme is exactly https.

Returns bool

public function withHost(string $host): RequestUrl

Returns a copy using the given host.

The host is stored as given, without trusted-host filtering — that is applied by the factories that read a host off the request.

ParameterTypeDescription
$hoststring

Returns RequestUrl

public function withPath(string $path): RequestUrl

Returns a copy using the given path.

The stored request URI is not rebuilt from it.

ParameterTypeDescription
$pathstring

Returns RequestUrl

public function withPort(int $port): RequestUrl

Returns a copy using the given port.

Pass 0 to leave the port unset, in which case RequestUrl::effectivePort() answers with the scheme’s default.

ParameterTypeDescription
$portint

Returns RequestUrl

public function withProtocol(?string $protocol): RequestUrl

Returns a copy using the given protocol string, e.g.

HTTP/1.1.

Pass null for a request whose protocol is unknown, such as one built outside a SAPI.

ParameterTypeDescription
$protocol?``string

Returns RequestUrl

public function withQuery(string $query): RequestUrl

Returns a copy using the given query string, without the leading ?.

The stored request URI is not rebuilt from it.

ParameterTypeDescription
$querystring

Returns RequestUrl

public function withRequestUri(string $requestUri): RequestUrl

Returns a copy whose request URI is the given string.

The path and query components are kept as they are, so a caller replacing the request URI is responsible for keeping them in step.

ParameterTypeDescription
$requestUristring

Returns RequestUrl

public function withScheme(string $scheme): RequestUrl

Returns a copy using the given scheme.

The stored request URI and full URL are carried over untouched, so they keep whatever they were derived from; RequestUrl::fullUrl() recomputes the absolute URL from the current scheme, host and port instead.

ParameterTypeDescription
$schemestring

Returns RequestUrl