Skip to main content

PSR-7 Methods

This library is fully compliant with PSR-7 UriInterface. All methods are marked with the #[Override] attribute.

Scheme

getScheme()

Retrieve the scheme component of the URI.

public function getScheme(): string

Returns: The URI scheme in lowercase (e.g., "http", "https", "ftp").

$uri = Uri::getInstance("HTTPS://example.com");
echo $uri->getScheme(); // "https" (normalized to lowercase)

withScheme()

Return an instance with the specified scheme.

public function withScheme(string $scheme): UriInterface

Parameters:

  • $scheme - The scheme to set (will be normalized to lowercase)

Returns: A new instance with the specified scheme.

$uri = Uri::getInstance("http://example.com");
$uri = $uri->withScheme("https");
echo (string)$uri; // "https://example.com"

Authority Components

getHost()

Retrieve the host component of the URI.

public function getHost(): string
$uri = Uri::getInstance("https://example.com:8080/path");
echo $uri->getHost(); // "example.com"

withHost()

Return an instance with the specified host.

public function withHost(string $host): UriInterface
$uri = $uri->withHost("newhost.com");

getPort()

Retrieve the port component of the URI.

public function getPort(): ?int

Returns: The port as an integer, or null if not set.

$uri = Uri::getInstance("https://example.com:8080");
echo $uri->getPort(); // 8080

$uri = Uri::getInstance("https://example.com");
var_dump($uri->getPort()); // NULL

withPort()

Return an instance with the specified port.

public function withPort(?int $port): UriInterface
$uri = $uri->withPort(9000);
$uri = $uri->withPort(null); // Remove port

getUserInfo()

Retrieve the user information component of the URI.

public function getUserInfo(): string

Returns: User information in "username[:password]" format. Password is URL-encoded.

$uri = Uri::getInstance("https://user:pa&[email protected]");
echo $uri->getUserInfo(); // "user:pa%26ss"

$uri = Uri::getInstance("https://[email protected]");
echo $uri->getUserInfo(); // "user"

$uri = Uri::getInstance("https://example.com");
echo $uri->getUserInfo(); // "" (empty string)

withUserInfo()

Return an instance with the specified user information.

public function withUserInfo(string $user, ?string $password = null): UriInterface
$uri = Uri::getInstance("https://example.com")
->withUserInfo("john", "secret");
echo (string)$uri; // "https://john:[email protected]"
warning

This method does NOT encode the password. If you pass an already-encoded password, it may result in double-encoding when the URI is serialized.

getAuthority()

Retrieve the authority component of the URI.

public function getAuthority(): string

Format: [userinfo@]host[:port]

$uri = Uri::getInstance("https://user:[email protected]:8080/path");
echo $uri->getAuthority(); // "user:[email protected]:8080"

Path

getPath()

Retrieve the path component of the URI.

public function getPath(): string
$uri = Uri::getInstance("https://example.com/some/path");
echo $uri->getPath(); // "/some/path"

withPath()

Return an instance with the specified path.

public function withPath(string $path): UriInterface
$uri = $uri->withPath("/new/path");

Query

getQuery()

Retrieve the query string of the URI.

public function getQuery(): string

Returns: The query string, RFC3986 encoded, without the leading "?".

$uri = Uri::getInstance("https://example.com?name=John&age=30");
echo $uri->getQuery(); // "name=John&age=30"

withQuery()

Return an instance with the specified query string.

public function withQuery(string $query): UriInterface

Parameters:

  • $query - The query string (without leading "?")
$uri = $uri->withQuery("search=test&limit=10");

Fragment

getFragment()

Retrieve the fragment component of the URI.

public function getFragment(): string

Returns: The URI fragment without the leading "#".

$uri = Uri::getInstance("https://example.com#section");
echo $uri->getFragment(); // "section"

withFragment()

Return an instance with the specified URI fragment.

public function withFragment(string $fragment): UriInterface
$uri = $uri->withFragment("top");

String Conversion

__toString()

Return the string representation of the URI.

public function __toString(): string

Format: [scheme]://[authority][path]?[query]#[fragment]

$uri = Uri::getInstance("https://user:[email protected]:8080/path?query=value#fragment");
echo (string)$uri;
// "https://user:[email protected]:8080/path?query=value#fragment"