Skip to content

SimpleUri

A minimal PSR-7 UriInterface built by handing a URI string to parse_url(), so the framework can supply a URI without depending on a third-party PSR-7 implementation.

WebRequest falls back to one of these (http://localhost/) when it is constructed without a URI, which is the usual case in tests and in dispatches assembled by hand rather than from an incoming HTTP request.

Deliberately thin: components are stored exactly as parsed or as supplied to the with*() methods. There is no percent-encoding, no case normalisation of scheme or host, no validation of the values passed in, and no default-port handling — a port equal to the scheme’s default is still reported by SimpleUri::getPort() and still appears in SimpleUri::getAuthority(). Every with*() method returns a clone.

class SimpleUri implements UriInterface

ImplementsUriInterface
SourceHttp/SimpleUri.php

public function __construct(string $uri): mixed

ParameterTypeDescription
$uristring

Returns mixed

MethodDescription
__toString(): stringReturn the string representation as a URI reference.
getAuthority(): stringReturns [user-info@]host[:port], omitting each part that is empty or unset.
getFragment(): stringReturns the fragment without its leading #, or an empty string when there is none.
getHost(): stringReturns the host component, or an empty string when the parsed URI had none.
getPath(): stringReturns the path component, or an empty string when the parsed URI had none.
getPort(): ?intReturns the port, or null when the URI did not state one; no default-port normalisation is applied.
getQuery(): stringReturns the query string without its leading ?, or an empty string when there is none.
getScheme(): stringReturns the scheme component, or an empty string when the parsed URI had none.
getUserInfo(): stringReturns user[:password], or an empty string when no user was present in the URI.
withFragment(mixed $fragment): staticReturns a clone carrying the given fragment, with any leading # stripped.
withHost(mixed $host): staticReturns a clone carrying the given host, stored verbatim.
withPath(mixed $path): staticReturns a clone carrying the given path, stored verbatim without encoding.
withPort(mixed $port): staticReturns a clone carrying the given port; null removes the port from the authority.
withQuery(mixed $query): staticReturns a clone carrying the given query, with any leading ? stripped.
withScheme(mixed $scheme): staticReturns a clone carrying the given scheme; the value is stored as supplied, without case or syntax normalisation.
withUserInfo(mixed $user, mixed $password = null): staticReturns a clone carrying the given user and password; a null password clears the stored password.

public function __toString(): string

Return the string representation as a URI reference.

Depending on which components of the URI are present, the resulting string is either a full URI or relative reference according to RFC 3986, Section 4.1. The method concatenates the various components of the URI, using the appropriate delimiters:

  • If a scheme is present, it MUST be suffixed by ”:”. - If an authority is present, it MUST be prefixed by ”//”. - The path can be concatenated without delimiters. But there are two cases where the path has to be adjusted to make the URI reference valid as PHP does not allow to throw an exception in __toString(): - If the path is rootless and an authority is present, the path MUST be prefixed by ”/”. - If the path is starting with more than one ”/” and no authority is present, the starting slashes MUST be reduced to one. - If a query is present, it MUST be prefixed by ”?”. - If a fragment is present, it MUST be prefixed by ”#”.

Returns string

public function getAuthority(): string

Returns [user-info@]host[:port], omitting each part that is empty or unset.

Returns string

public function getFragment(): string

Returns the fragment without its leading #, or an empty string when there is none.

Returns string

public function getHost(): string

Returns the host component, or an empty string when the parsed URI had none.

Returns string

public function getPath(): string

Returns the path component, or an empty string when the parsed URI had none.

Returns string

public function getPort(): ?int

Returns the port, or null when the URI did not state one; no default-port normalisation is applied.

Returns ?``int

public function getQuery(): string

Returns the query string without its leading ?, or an empty string when there is none.

Returns string

public function getScheme(): string

Returns the scheme component, or an empty string when the parsed URI had none.

Returns string

public function getUserInfo(): string

Returns user[:password], or an empty string when no user was present in the URI.

Returns string

public function withFragment(mixed $fragment): static

Returns a clone carrying the given fragment, with any leading # stripped.

ParameterTypeDescription
$fragmentmixed

Returns static

public function withHost(mixed $host): static

Returns a clone carrying the given host, stored verbatim.

ParameterTypeDescription
$hostmixed

Returns static

public function withPath(mixed $path): static

Returns a clone carrying the given path, stored verbatim without encoding.

ParameterTypeDescription
$pathmixed

Returns static

public function withPort(mixed $port): static

Returns a clone carrying the given port; null removes the port from the authority.

ParameterTypeDescription
$portmixed

Returns static

public function withQuery(mixed $query): static

Returns a clone carrying the given query, with any leading ? stripped.

ParameterTypeDescription
$querymixed

Returns static

public function withScheme(mixed $scheme): static

Returns a clone carrying the given scheme; the value is stored as supplied, without case or syntax normalisation.

ParameterTypeDescription
$schememixed

Returns static

public function withUserInfo(mixed $user, mixed $password = null): static

Returns a clone carrying the given user and password; a null password clears the stored password.

ParameterTypeDescription
$usermixed
$passwordmixed

Returns static