The Authority part

The Authority class represents a URI authority component. Apart from the package common API the class exposes basic properties and method to manipulate its different component.

If the modifications do not change the current object, it is returned as is, otherwise, a new modified object is returned.

If the submitted value is not valid a League\Uri\Exceptions\SyntaxError exception is thrown.


The Authority class comes with named constructors to ease instantiation. The following examples show how to instantiate the class:

submitted string is normalized to be RFC3986 compliant.


use League\Uri\Components\Authority;
use League\Uri\UriString;

$authority = new Authority('eXamPle.cOm', 42, 'user:pass');
$authority->toString(); //returns ''

Authority::new('')->value(); //returns ''
Authority::fromUri("")->getPort(); //return null;
Authority::new()->value(); //return null;
)->value(); //returns ''

if no string is given a instance is returns using the empty string.

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

Accessing properties

You can access the authority string, its individual parts and components using their respective getter methods. This lead to the following result for a simple HTTP URI:

use League\Uri\Components\Authority;

$authority = Authority::new("");
echo $authority->getUserInfo();  //displays "foo:bar"
echo $authority->getHost();      //displays ""
echo $authority->getPort();      //displays 81 as an integer
echo $authority;
//displays ""
echo json_encode($authority);
//displays ""
// returns array {
//   "user" => "foo",
//   "pass" => "bar",
//   "host" => "",
//   "port" => 81,
// }

Modifying properties

To replace one of the URI component 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\Contracts\UriException exception if the resulting URI is not valid. Just like with the instantiation methods, validation is scheme dependant.

Since All URI object are immutable you can chain each modifying methods to simplify URI creation and/or modification.

echo Authority::new("")
    ->withUserInfo("foo", "bar")
    ->toString(); //displays "//"


Out of the box the package normalizes the URI part according to the non-destructive rules of RFC3986.

These non-destructive rules are:

echo Authority::new(""); //displays

Host conversion depends on the presence of the idn_to_* functions, if missing the code will trigger a MissingFeature exception