Class Uri
Immutable PSR-7 URI using PHP's RFC 3986 parser.
Components preserve percent-encoded sequences and dot segments. Non-ASCII input is percent-encoded before parsing.
Constants
-
stringCHAR_SUB_DELIMS ¶'!\$&\'\(\)\*\+,;='Sub-delimiter characters as defined in RFC 3986 section 2.2.
-
stringCHAR_UNRESERVED ¶'a-zA-Z0-9_\-\.~'Unreserved characters as defined in RFC 3986 section 2.3.
-
array<string, int>DEFAULT_PORTS ¶['http' => 80, 'https' => 443]Default ports for well known schemes.
Property Summary
Method Summary
-
__construct() public
Constructor
-
__toString() public
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 "#".
-
encode() protected
Percent-encode all characters matching a pattern.
Non-ASCII bytes are always encoded.
-
filterFragment() protected
Percent-encode a fragment.
-
filterPath() protected
Percent-encode a path, leaving already encoded sequences intact.
-
filterQuery() protected
Percent-encode a query string, preserving its delimiters.
-
filterQueryOrFragment() protected
Percent-encode a query key, query value or fragment.
-
filterUserInfoPart() protected
Percent-encode a user or password component.
-
getAuthority() public
Retrieve the authority component of the URI.
If no authority information is present, this method MUST return an empty string.
The authority syntax of the URI is:
[user-info@]host[:port]
If the port component is not set or is the standard port for the current scheme, it SHOULD NOT be included.
-
getFragment() public
Retrieve the fragment component of the URI.
If no fragment is present, this method MUST return an empty string.
The leading "#" character is not part of the fragment and MUST NOT be added.
The value returned MUST be percent-encoded, but MUST NOT double-encode any characters. To determine what characters to encode, please refer to RFC 3986, Sections 2 and 3.5.
-
getHost() public
Retrieve the host component of the URI.
If no host is present, this method MUST return an empty string.
The value returned MUST be normalized to lowercase, per RFC 3986 Section 3.2.2.
-
getPath() public
Retrieve the path component of the URI.
The path can either be empty or absolute (starting with a slash) or rootless (not starting with a slash). Implementations MUST support all three syntaxes.
Normally, the empty path "" and absolute path "/" are considered equal as defined in RFC 7230 Section 2.7.3. But this method MUST NOT automatically do this normalization because in contexts with a trimmed base path, e.g. the front controller, this difference becomes significant. It's the task of the user to handle both "" and "/".
The value returned MUST be percent-encoded, but MUST NOT double-encode any characters. To determine what characters to encode, please refer to RFC 3986, Sections 2 and 3.3.
As an example, if the value should include a slash ("/") not intended as delimiter between path segments, that value MUST be passed in encoded form (e.g., "%2F") to the instance.
-
getPort() public
Retrieve the port component of the URI.
If a port is present, and it is non-standard for the current scheme, this method MUST return it as an integer. If the port is the standard port used with the current scheme, this method SHOULD return null.
If no port is present, and no scheme is present, this method MUST return a null value.
If no port is present, but a scheme is present, this method MAY return the standard port for that scheme, but SHOULD return null.
-
getQuery() public
Retrieve the query string of the URI.
If no query string is present, this method MUST return an empty string.
The leading "?" character is not part of the query and MUST NOT be added.
The value returned MUST be percent-encoded, but MUST NOT double-encode any characters. To determine what characters to encode, please refer to RFC 3986, Sections 2 and 3.4.
As an example, if a value in a key/value pair of the query string should include an ampersand ("&") not intended as a delimiter between values, that value MUST be passed in encoded form (e.g., "%26") to the instance.
-
getScheme() public
Retrieve the scheme component of the URI.
If no scheme is present, this method MUST return an empty string.
The value returned MUST be normalized to lowercase, per RFC 3986 Section 3.1.
The trailing ":" character is not part of the scheme and MUST NOT be added.
-
getUserInfo() public
Retrieve the user information component of the URI.
If no user information is present, this method MUST return an empty string.
If a user is present in the URI, this will return that value; additionally, if the password is also present, it will be appended to the user value, with a colon (":") separating the values.
The trailing "@" character is not part of the user information and MUST NOT be added.
-
withFragment() public
Return an instance with the specified URI fragment.
This method MUST retain the state of the current instance, and return an instance that contains the specified URI fragment.
Users can provide both encoded and decoded fragment characters. Implementations ensure the correct encoding as outlined in getFragment().
An empty fragment value is equivalent to removing the fragment.
-
withHost() public
Return an instance with the specified host.
This method MUST retain the state of the current instance, and return an instance that contains the specified host.
An empty host value is equivalent to removing the host.
-
withNativeUri() protected
Apply a native mutation using PSR-7 exception semantics.
-
withPath() public
Return an instance with the specified path.
This method MUST retain the state of the current instance, and return an instance that contains the specified path.
The path can either be empty or absolute (starting with a slash) or rootless (not starting with a slash). Implementations MUST support all three syntaxes.
If the path is intended to be domain-relative rather than path relative then it must begin with a slash ("/"). Paths not starting with a slash ("/") are assumed to be relative to some base path known to the application or consumer.
Users can provide both encoded and decoded path characters. Implementations ensure the correct encoding as outlined in getPath().
-
withPort() public
Return an instance with the specified port.
This method MUST retain the state of the current instance, and return an instance that contains the specified port.
Implementations MUST raise an exception for ports outside the established TCP and UDP port ranges.
A null value provided for the port is equivalent to removing the port information.
-
withQuery() public
Return an instance with the specified query string.
This method MUST retain the state of the current instance, and return an instance that contains the specified query string.
Users can provide both encoded and decoded query characters. Implementations ensure the correct encoding as outlined in getQuery().
An empty query string value is equivalent to removing the query string.
-
withScheme() public
Return an instance with the specified scheme.
This method MUST retain the state of the current instance, and return an instance that contains the specified scheme.
Implementations MUST support the schemes "http" and "https" case insensitively, and MAY accommodate other schemes if required.
An empty scheme is equivalent to removing the scheme.
-
withUserInfo() public
Return an instance with the specified user information.
This method MUST retain the state of the current instance, and return an instance that contains the specified user information.
Password is optional, but the user information MUST include the user; an empty string for the user is equivalent to removing user information.
Method Detail
__construct() ¶ public
__construct(Uri\Rfc3986\Uri|Uri\WhatWg\Url|string $uri = '')
Constructor
Parameters
-
Uri\Rfc3986\Uri|Uri\WhatWg\Url|string$uri optional The URI to parse or adapt to PSR-7.
Throws
InvalidArgumentExceptionWhen the URI cannot be parsed.
__toString() ¶ public
__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
stringencode() ¶ protected
encode(string $value, string $pattern): string
Percent-encode all characters matching a pattern.
Non-ASCII bytes are always encoded.
Parameters
-
string$value The value to encode.
-
string$pattern The pattern matching the characters to encode.
Returns
stringfilterFragment() ¶ protected
filterFragment(string $fragment): string
Percent-encode a fragment.
Parameters
-
string$fragment The fragment to filter.
Returns
stringfilterPath() ¶ protected
filterPath(string $path): string
Percent-encode a path, leaving already encoded sequences intact.
Parameters
-
string$path The path to filter.
Returns
stringfilterQuery() ¶ protected
filterQuery(string $query): string
Percent-encode a query string, preserving its delimiters.
Parameters
-
string$query The query string to filter.
Returns
stringfilterQueryOrFragment() ¶ protected
filterQueryOrFragment(string $value): string
Percent-encode a query key, query value or fragment.
Parameters
-
string$value The value to filter.
Returns
stringfilterUserInfoPart() ¶ protected
filterUserInfoPart(string $part): string
Percent-encode a user or password component.
Parameters
-
string$part The user info part to filter.
Returns
stringgetAuthority() ¶ public
getAuthority(): string
Retrieve the authority component of the URI.
If no authority information is present, this method MUST return an empty string.
The authority syntax of the URI is:
[user-info@]host[:port]
If the port component is not set or is the standard port for the current scheme, it SHOULD NOT be included.
Returns
stringgetFragment() ¶ public
getFragment(): string
Retrieve the fragment component of the URI.
If no fragment is present, this method MUST return an empty string.
The leading "#" character is not part of the fragment and MUST NOT be added.
The value returned MUST be percent-encoded, but MUST NOT double-encode any characters. To determine what characters to encode, please refer to RFC 3986, Sections 2 and 3.5.
Returns
stringgetHost() ¶ public
getHost(): string
Retrieve the host component of the URI.
If no host is present, this method MUST return an empty string.
The value returned MUST be normalized to lowercase, per RFC 3986 Section 3.2.2.
Returns
stringgetPath() ¶ public
getPath(): string
Retrieve the path component of the URI.
The path can either be empty or absolute (starting with a slash) or rootless (not starting with a slash). Implementations MUST support all three syntaxes.
Normally, the empty path "" and absolute path "/" are considered equal as defined in RFC 7230 Section 2.7.3. But this method MUST NOT automatically do this normalization because in contexts with a trimmed base path, e.g. the front controller, this difference becomes significant. It's the task of the user to handle both "" and "/".
The value returned MUST be percent-encoded, but MUST NOT double-encode any characters. To determine what characters to encode, please refer to RFC 3986, Sections 2 and 3.3.
As an example, if the value should include a slash ("/") not intended as delimiter between path segments, that value MUST be passed in encoded form (e.g., "%2F") to the instance.
Returns
stringgetPort() ¶ public
getPort(): null|int
Retrieve the port component of the URI.
If a port is present, and it is non-standard for the current scheme, this method MUST return it as an integer. If the port is the standard port used with the current scheme, this method SHOULD return null.
If no port is present, and no scheme is present, this method MUST return a null value.
If no port is present, but a scheme is present, this method MAY return the standard port for that scheme, but SHOULD return null.
Returns
null|intgetQuery() ¶ public
getQuery(): string
Retrieve the query string of the URI.
If no query string is present, this method MUST return an empty string.
The leading "?" character is not part of the query and MUST NOT be added.
The value returned MUST be percent-encoded, but MUST NOT double-encode any characters. To determine what characters to encode, please refer to RFC 3986, Sections 2 and 3.4.
As an example, if a value in a key/value pair of the query string should include an ampersand ("&") not intended as a delimiter between values, that value MUST be passed in encoded form (e.g., "%26") to the instance.
Returns
stringgetScheme() ¶ public
getScheme(): string
Retrieve the scheme component of the URI.
If no scheme is present, this method MUST return an empty string.
The value returned MUST be normalized to lowercase, per RFC 3986 Section 3.1.
The trailing ":" character is not part of the scheme and MUST NOT be added.
Returns
stringgetUserInfo() ¶ public
getUserInfo(): string
Retrieve the user information component of the URI.
If no user information is present, this method MUST return an empty string.
If a user is present in the URI, this will return that value; additionally, if the password is also present, it will be appended to the user value, with a colon (":") separating the values.
The trailing "@" character is not part of the user information and MUST NOT be added.
Returns
stringwithFragment() ¶ public
withFragment(string $fragment): static
Return an instance with the specified URI fragment.
This method MUST retain the state of the current instance, and return an instance that contains the specified URI fragment.
Users can provide both encoded and decoded fragment characters. Implementations ensure the correct encoding as outlined in getFragment().
An empty fragment value is equivalent to removing the fragment.
Parameters
-
string$fragment
Returns
staticwithHost() ¶ public
withHost(string $host): static
Return an instance with the specified host.
This method MUST retain the state of the current instance, and return an instance that contains the specified host.
An empty host value is equivalent to removing the host.
Parameters
-
string$host
Returns
staticwithNativeUri() ¶ protected
withNativeUri(callable(Uri\Rfc3986\Uri): \Uri\Rfc3986\Uri $mutator): static
Apply a native mutation using PSR-7 exception semantics.
Parameters
-
callable(Uri\Rfc3986\Uri): \Uri\Rfc3986\Uri$mutator The native mutation.
Returns
staticThrows
InvalidArgumentExceptionWhen a component is invalid.
withPath() ¶ public
withPath(string $path): static
Return an instance with the specified path.
This method MUST retain the state of the current instance, and return an instance that contains the specified path.
The path can either be empty or absolute (starting with a slash) or rootless (not starting with a slash). Implementations MUST support all three syntaxes.
If the path is intended to be domain-relative rather than path relative then it must begin with a slash ("/"). Paths not starting with a slash ("/") are assumed to be relative to some base path known to the application or consumer.
Users can provide both encoded and decoded path characters. Implementations ensure the correct encoding as outlined in getPath().
Parameters
-
string$path
Returns
staticwithPort() ¶ public
withPort(null|int $port): static
Return an instance with the specified port.
This method MUST retain the state of the current instance, and return an instance that contains the specified port.
Implementations MUST raise an exception for ports outside the established TCP and UDP port ranges.
A null value provided for the port is equivalent to removing the port information.
Parameters
-
null|int$port
Returns
staticwithQuery() ¶ public
withQuery(string $query): static
Return an instance with the specified query string.
This method MUST retain the state of the current instance, and return an instance that contains the specified query string.
Users can provide both encoded and decoded query characters. Implementations ensure the correct encoding as outlined in getQuery().
An empty query string value is equivalent to removing the query string.
Parameters
-
string$query
Returns
staticwithScheme() ¶ public
withScheme(string $scheme): static
Return an instance with the specified scheme.
This method MUST retain the state of the current instance, and return an instance that contains the specified scheme.
Implementations MUST support the schemes "http" and "https" case insensitively, and MAY accommodate other schemes if required.
An empty scheme is equivalent to removing the scheme.
Parameters
-
string$scheme
Returns
staticwithUserInfo() ¶ public
withUserInfo(string $user, null|string $password = null): static
Return an instance with the specified user information.
This method MUST retain the state of the current instance, and return an instance that contains the specified user information.
Password is optional, but the user information MUST include the user; an empty string for the user is equivalent to removing user information.
Parameters
-
string$user -
null|string$password optional
Returns
static