--- layout: default title: Uri Object API redirect_from: - /5.0/uri/api/ --- URI Objects API ======= The URI objects are mainly used to validate and normalized URI against `RFC3986` and each scheme specific validation rules.

Starting with version 1.1.0 all URI objects are defined in the League\Uri namespace. The League\Uri\Schemes namespace is deprecated and will be removed in the next major release.

Creating new URI objects ------- ### URI instantiation To instantiate a new URI object you can use two named constructors: ~~~php The default constructor is protected and can not be accessed to instantiate a new object.

If you supply your own hash to createFromComponents, you are responsible for providing well parsed components without their URI delimiters.

A `League\Uri\UriException` exception is triggered if an invalid URI is given. ~~~php Because the League\Uri\UriException exception extends League\Uri\Exception you can catch any exception triggered by the package using the following code.

League\Uri\Exception extends PHP's SPL InvalidArgumentException.

~~~php getScheme(); //displays "http" echo $uri->getUserInfo(); //displays "foo:bar" echo $uri->getHost(); //displays "www.example.com" echo $uri->getPort(); //displays 81 as an integer echo $uri->getAuthority(); //displays "foo:bar@www.example.com:81" echo $uri->getPath(); //displays "/how/are/you" echo $uri->getQuery(); //displays "foo=baz" echo $uri->getFragment(); //displays "title" ~~~ Modifying URI properties ------- To replace one of the URI part you can use the modifying methods exposed by all URI object. If the modifications do not alter the current object, it is returned as is, otherwise, a new modified object is returned.

Any modification method can trigger a League\Uri\UriException exception if the resulting URI is not valid. Just like with the instantiation methods, validition is scheme dependant.

~~~php withScheme("wss") ->withUserInfo("foo", "bar") ->withHost("www.example.com") ->withPort(81) ->withPath("/how/are/you") ->withQuery("foo=baz"); echo $uri; //displays wss://foo:bar@www.example.com:81/how/are/you?foo=baz ~~~ URI normalization ------- Out of the box the package normalizes any given URI according to the non destructive rules of RFC3986. These non destructives rules are: - scheme and host components are lowercased; - the host is converted to its ascii representation using punycode if needed - query, path, fragment components are URI encoded if needed; - the port number is removed from the URI string representation if the standard port is used; ~~~php