Overview
  • Namespace
  • Class

Namespaces

  • Slim
    • Exception
    • Handlers
      • Strategies
    • Http
    • Interfaces
      • Http

Classes

  • Body
  • Cookies
  • Environment
  • Headers
  • Message
  • Request
  • RequestBody
  • Response
  • Stream
  • UploadedFile
  • Uri

Class Uri

Value object representing a URI.

This interface is meant to represent URIs according to RFC 3986 and to provide methods for most common operations. Additional functionality for working with URIs can be provided on top of the interface or externally. Its primary use is for HTTP requests, but may also be used in other contexts.

Instances of this interface are considered immutable; all methods that might change state MUST be implemented such that they retain the internal state of the current instance and return an instance that contains the changed state.

Typically the Host header will be also be present in the request message. For server-side requests, the scheme will typically be discoverable in the server parameters.

Slim\Http\Uri implements Psr\Http\Message\UriInterface
Namespace: Slim\Http
Link: (the URI specification)
Located at Http/Uri.php

Methods summary

public
# __construct( string $scheme, string $host, integer $port = null, string $path = '/', string $query = '', string $fragment = '', string $user = '', string $password = '' )

Create new Uri.

Create new Uri.

Parameters

$scheme
Uri scheme.
$host
Uri host.
$port
Uri port number.
$path
Uri path.
$query
Uri query string.
$fragment
Uri fragment.
$user
Uri user.
$password
Uri password.
public static Slim\Http\Uri
# createFromString( string $uri )

Create new Uri from string.

Create new Uri from string.

Parameters

$uri

Complete Uri string (i.e., https://user:pass@host:443/path?query).

Returns

Slim\Http\Uri
public static Slim\Http\Uri
# createFromEnvironment( Slim\Http\Environment $env )

Create new Uri from environment.

Create new Uri from environment.

Parameters

$env

Returns

Slim\Http\Uri
public string
# getScheme( )

Retrieve the scheme component of the URI.

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

string
The URI scheme.

See

https://tools.ietf.org/html/rfc3986#section-3.1
public Slim\Http\Uri
# withScheme( string $scheme )

Return an instance with the specified scheme.

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

$scheme
The scheme to use with the new instance.

Returns

Slim\Http\Uri
A new instance with the specified scheme.

Throws

InvalidArgumentException
for invalid or unsupported schemes.
protected string
# filterScheme( string $scheme )

Filter Uri scheme.

Filter Uri scheme.

Parameters

$scheme
Raw Uri scheme.

Returns

string

Throws

InvalidArgumentException
If the Uri scheme is not a string.
InvalidArgumentException
If Uri scheme is not "", "https", or "http".
public string
# getAuthority( )

Retrieve the authority component of the URI.

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

string
The URI authority, in "[user-info@]host[:port]" format.

See

https://tools.ietf.org/html/rfc3986#section-3.2
public string
# getUserInfo( )

Retrieve the user information component of the URI.

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

string
The URI user information, in "username[:password]" format.
public Slim\Http\Uri
# withUserInfo( string $user, null|string $password = null )

Return an instance with the specified user information.

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

$user
The user name to use for authority.
$password
The password associated with $user.

Returns

Slim\Http\Uri
A new instance with the specified user information.
public string
# getHost( )

Retrieve the host component of the URI.

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

string
The URI host.

See

http://tools.ietf.org/html/rfc3986#section-3.2.2
public Slim\Http\Uri
# withHost( string $host )

Return an instance with the specified host.

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

$host
The hostname to use with the new instance.

Returns

Slim\Http\Uri
A new instance with the specified host.

Throws

InvalidArgumentException
for invalid hostnames.
public null|integer
# getPort( )

Retrieve the port component of the URI.

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|integer
The URI port.
public Slim\Http\Uri
# withPort( null|integer $port )

Return an instance with the specified port.

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

$port

The port to use with the new instance; a null value removes the port information.

Returns

Slim\Http\Uri
A new instance with the specified port.

Throws

InvalidArgumentException
for invalid ports.
protected boolean
# hasStandardPort( )

Does this Uri use a standard port?

Does this Uri use a standard port?

Returns

boolean
protected null|integer
# filterPort( null|integer $port )

Filter Uri port.

Filter Uri port.

Parameters

$port
The Uri port number.

Returns

null|integer

Throws

InvalidArgumentException
If the port is invalid.
public string
# getPath( )

Retrieve the path component of the URI.

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

string
The URI path.

See

https://tools.ietf.org/html/rfc3986#section-2
https://tools.ietf.org/html/rfc3986#section-3.3
public Slim\Http\Uri
# withPath( string $path )

Return an instance with the specified path.

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

$path
The path to use with the new instance.

Returns

Slim\Http\Uri
A new instance with the specified path.

Throws

InvalidArgumentException
for invalid paths.
public string
# getBasePath( )

Retrieve the base path segment of the URI.

Retrieve the base path segment of the URI.

Note: This method is not part of the PSR-7 standard.

This method MUST return a string; if no path is present it MUST return an empty string.

Returns

string
The base path segment of the URI.
public Slim\Http\Uri
# withBasePath( string $basePath )

Set base path.

Set base path.

Note: This method is not part of the PSR-7 standard.

Parameters

$basePath

Returns

Slim\Http\Uri
protected string
# filterPath( string $path )

Filter Uri path.

Filter Uri path.

This method percent-encodes all reserved characters in the provided path string. This method will NOT double-encode characters that are already percent-encoded.

Parameters

$path
The raw uri path.

Returns

string
The RFC 3986 percent-encoded uri path.

Link

http://www.faqs.org/rfcs/rfc3986.html
public string
# getQuery( )

Retrieve the query string of the URI.

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

string
The URI query string.

See

https://tools.ietf.org/html/rfc3986#section-2
https://tools.ietf.org/html/rfc3986#section-3.4
public Slim\Http\Uri
# withQuery( string $query )

Return an instance with the specified query string.

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

$query
The query string to use with the new instance.

Returns

Slim\Http\Uri
A new instance with the specified query string.

Throws

InvalidArgumentException
for invalid query strings.
protected string
# filterQuery( string $query )

Filters the query string or fragment of a URI.

Filters the query string or fragment of a URI.

Parameters

$query
The raw uri query string.

Returns

string
The percent-encoded query string.
public string
# getFragment( )

Retrieve the fragment component of the URI.

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

string
The URI fragment.

See

https://tools.ietf.org/html/rfc3986#section-2
https://tools.ietf.org/html/rfc3986#section-3.5
public Slim\Http\Uri
# withFragment( string $fragment )

Return an instance with the specified URI fragment.

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

$fragment
The fragment to use with the new instance.

Returns

Slim\Http\Uri
A new instance with the specified fragment.
public string
# __toString( )

Return the string representation as a URI reference.

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

See

http://tools.ietf.org/html/rfc3986#section-4.1
public string
# getBaseUrl( )

Return the fully qualified base URL.

Return the fully qualified base URL.

Note that this method never includes a trailing /

This method is not part of PSR-7.

Returns

string

Properties summary

protected string $scheme

Uri scheme (without "://" suffix)

Uri scheme (without "://" suffix)

# ''
protected string $user

Uri user

Uri user

# ''
protected string $password

Uri password

Uri password

# ''
protected string $host

Uri host

Uri host

# ''
protected null|integer $port

Uri port number

Uri port number

#
protected string $basePath

Uri base path

Uri base path

# ''
protected string $path

Uri path

Uri path

# ''
protected string $query

Uri query string (without "?" prefix)

Uri query string (without "?" prefix)

# ''
protected string $fragment

Uri fragment string (without "#" prefix)

Uri fragment string (without "#" prefix)

# ''
API documentation generated by ApiGen