Class Uri

Summary

Fully Qualified Name: Zend\Uri\Uri
Implements: UriInterface

Description

Generic URI handler

Methods

Name Description Defined By
__construct() Create a new URI object Uri
__toString() Magic method to convert the URI to a string Uri
encodePath() Encode the path Uri
encodeQueryFragment() URL-encode a query string or fragment based on RFC-3986 guidelines. Uri
encodeUserInfo() URL-encode the user info part of a URI Uri
getEscaper() Retrieve Escaper instance Uri
getFragment() Get the URI fragment Uri
getHost() Get the URI host Uri
getPath() Get the URI path Uri
getPort() Get the URI port Uri
getQuery() Get the URI query Uri
getQueryAsArray() Return the query string as an associative array of key => value pairs Uri
getScheme() Get the scheme part of the URI Uri
getUserInfo() Get the User-info (usually user:password) part Uri
isAbsolute() Check if the URI is an absolute or relative URI Uri
isValid() Check if the URI is valid Uri
isValidRelative() Check if the URI is a valid relative URI Uri
makeRelative() Convert the link to a relative link by substracting a base URI Uri
merge() Merge a base URI and a relative URI into a new URI object Uri
normalize() Normalize the URI Uri
parse() Parse a URI string Uri
parseScheme() Extract only the scheme part out of a URI string. Uri
removePathDotSegments() Remove any extra dot segments (/. Uri
resolve() Convert a relative URI into an absolute URI using a base absolute URI as a reference. Uri
setEscaper() Set Escaper instance Uri
setFragment() Set the URI fragment part Uri
setHost() Set the URI host Uri
setPath() Set the path Uri
setPort() Set the port part of the URI Uri
setQuery() Set the query string Uri
setScheme() Set the URI scheme Uri
setUserInfo() Set the URI User-info part (usually user:password) Uri
toString() Compose the URI into a string Uri
validateHost() Validate the host part Uri
validatePath() Validate the path Uri
validatePort() Validate the port Uri
validateQueryFragment() Check if a URI query or fragment part is valid or not Uri
validateScheme() Check if a scheme is valid or not Uri
validateUserInfo() Check that the userInfo part of a URI is valid Uri

Method Details

__construct()

Create a new URI object

Parameter Name Type Description
$uri \Uri|string|null

Returns:

__toString()

Magic method to convert the URI to a string

Returns: string

encodePath()

Encode the path

Will replace all characters which are not strictly allowed in the path part with percent-encoded representation

Parameter Name Type Description
$path string

Returns: string

encodeQueryFragment()

URL-encode a query string or fragment based on RFC-3986 guidelines.

Note that query and fragment encoding allows more unencoded characters than the usual rawurlencode() function would usually return - for example '/' and ':' are allowed as literals.

Parameter Name Type Description
$input string

Returns: string

encodeUserInfo()

URL-encode the user info part of a URI

Parameter Name Type Description
$userInfo string

Returns: string

getEscaper()

Retrieve Escaper instance

Lazy-loads one if none provided

Returns: \Escaper

getFragment()

Get the URI fragment

Returns: string|null

getHost()

Get the URI host

Returns: string|null

getPath()

Get the URI path

Returns: string|null

getPort()

Get the URI port

Returns: int|null

getQuery()

Get the URI query

Returns: string|null

getQueryAsArray()

Return the query string as an associative array of key => value pairs

This is an extension to RFC-3986 but is quite useful when working with most common URI types

Returns: array

getScheme()

Get the scheme part of the URI

Returns: string|null

getUserInfo()

Get the User-info (usually user:password) part

Returns: string|null

isAbsolute()

Check if the URI is an absolute or relative URI

Returns: bool

isValid()

Check if the URI is valid

Note that a relative URI may still be valid

Returns: bool

isValidRelative()

Check if the URI is a valid relative URI

Returns: bool

makeRelative()

Convert the link to a relative link by substracting a base URI

This is the opposite of resolving a relative link - i.e. creating a relative reference link from an original URI and a base URI.

If the two URIs do not intersect (e.g. the original URI is not in any way related to the base URI) the URI will not be modified.

Parameter Name Type Description
$baseUri \Uri|string

Returns: \Uri

merge()

Merge a base URI and a relative URI into a new URI object

This convenience method wraps ::resolve() to allow users to quickly create new absolute URLs without the need to instantiate and clone URI objects.

If objects are passed in, none of the passed objects will be modified.

Parameter Name Type Description
$baseUri \Uri|string
$relativeUri \Uri|string

Returns: \Uri

normalize()

Normalize the URI

Normalizing a URI includes removing any redundant parent directory or current directory references from the path (e.g. foo/bar/../baz becomes foo/baz), normalizing the scheme case, decoding any over-encoded characters etc.

Eventually, two normalized URLs pointing to the same resource should be equal even if they were originally represented by two different strings

Returns: \Uri

parse()

Parse a URI string

Parameter Name Type Description
$uri string

Returns: \Uri

parseScheme()

Extract only the scheme part out of a URI string.

This is used by the parse() method, but is useful as a standalone public method if one wants to test a URI string for it's scheme before doing anything with it.

Will return the scheme if found, or NULL if no scheme found (URI may still be valid, but not full)

Parameter Name Type Description
$uriString string

Returns: string|null

removePathDotSegments()

Remove any extra dot segments (/.

./, /./) from a path

Algorithm is adapted from RFC-3986 section 5.2.4 (@link http://tools.ietf.org/html/rfc3986#section-5.2.4)

Parameter Name Type Description
$path string

Returns: string

resolve()

Convert a relative URI into an absolute URI using a base absolute URI as a reference.

This is similar to merge() - only it uses the supplied URI as the base reference instead of using the current URI as the base reference.

Merging algorithm is adapted from RFC-3986 section 5.2 (@link http://tools.ietf.org/html/rfc3986#section-5.2)

Parameter Name Type Description
$baseUri \Uri|string

Returns: \Uri

setEscaper()

Set Escaper instance

Parameter Name Type Description
$escaper \Escaper

Returns:

setFragment()

Set the URI fragment part

Parameter Name Type Description
$fragment string|null

Returns: \Uri

setHost()

Set the URI host

Note that the generic syntax for URIs allows using host names which are not necessarily IPv4 addresses or valid DNS host names. For example, IPv6 addresses are allowed as well, and also an abstract "registered name" which may be any name composed of a valid set of characters, including, for example, tilda (~) and underscore (_) which are not allowed in DNS names.

Subclasses of Uri may impose more strict validation of host names - for example the HTTP RFC clearly states that only IPv4 and valid DNS names are allowed in HTTP URIs.

Parameter Name Type Description
$host string|null

Returns: \Uri

setPath()

Set the path

Parameter Name Type Description
$path string|null

Returns: \Uri

setPort()

Set the port part of the URI

Parameter Name Type Description
$port int|null

Returns: \Uri

setQuery()

Set the query string

If an array is provided, will encode this array of parameters into a query string. Array values will be represented in the query string using PHP's common square bracket notation.

Parameter Name Type Description
$query string|array|null

Returns: \Uri

setScheme()

Set the URI scheme

If the scheme is not valid according to the generic scheme syntax or is not acceptable by the specific URI class (e.g. 'http' or 'https' are the only acceptable schemes for the Zend\Uri\Http class) an exception will be thrown.

You can check if a scheme is valid before setting it using the validateScheme() method.

Parameter Name Type Description
$scheme string|null

Returns: \Uri

setUserInfo()

Set the URI User-info part (usually user:password)

Parameter Name Type Description
$userInfo string|null

Returns: \Uri

toString()

Compose the URI into a string

Returns: string

validateHost()

Validate the host part

Users may control which host types to allow by passing a second parameter with a bitmask of HOST_* constants which are allowed. If not specified, all address types will be allowed.

Note that the generic URI syntax allows different host representations, including IPv4 addresses, IPv6 addresses and future IP address formats enclosed in square brackets, and registered names which may be DNS names or even more complex names. This is different (and is much more loose) from what is commonly accepted as valid HTTP URLs for example.

Parameter Name Type Description
$host string
$allowed int bitmask

Returns: bool

validatePath()

Validate the path

Parameter Name Type Description
$path string

Returns: bool

validatePort()

Validate the port

Valid values include numbers between 1 and 65535, and empty values

Parameter Name Type Description
$port int

Returns: bool

validateQueryFragment()

Check if a URI query or fragment part is valid or not

Query and Fragment parts are both restricted by the same syntax rules, so the same validation method can be used for both.

You can encode a query or fragment part to ensure it is valid by passing it through the encodeQueryFragment() method.

Parameter Name Type Description
$input string

Returns: bool

validateScheme()

Check if a scheme is valid or not

Will check $scheme to be valid against the generic scheme syntax defined in RFC-3986. If the class also defines specific acceptable schemes, will also check that $scheme is one of them.

Parameter Name Type Description
$scheme string

Returns: bool

validateUserInfo()

Check that the userInfo part of a URI is valid

Parameter Name Type Description
$userInfo string

Returns: bool

Top