--- layout: default title: URI components redirect_from: - /5.0/components/host/ --- The Host ======= The library provides a `Host` class to ease host creation and manipulation. This URI component object exposes the [package common API](/components/1.0/api/), but also provide specific methods to work with the URI host component.
## Creating a new object using the default constructor ~~~php since version1.7.0
you can inject a Rules object on instantiation.
The `League\Uri\Components\Exception` extends PHP's SPL `InvalidArgumentException`.
## Host represented by an IP
~~~php
since version 1.7.0
you can inject a Rules object on instantiation. The parameter is optional
~~~php
isIp(); //return false;
$ip_host = $host->withContent('127.0.0.1');
$ip_host->isIp(); //return true;
~~~
Knowing that you are dealing with an IP is good, knowing its version is better.
~~~php
isIp(); //return true
$ipv6->isIpv4(); //return false
$ipv6->isIpv6(); //return true
$ipv6->isIpFuture(); //return false
$ipv6->getIpVersion(); //return '6'
$ipv4 = new Host('127.0.0.1');
$ipv4->isIp(); //return true
$ipv4->isIpv4(); //return true
$ipv4->isIpv6(); //return false
$ipv4->isIpFuture(); //return false
$ipv4->getIpVersion(); //return '4'
$ipfuture = new Host('v32.1.2.3.4');
$ipfuture->isIp(); //return true
$ipfuture->isIpv4(); //return false
$ipfuture->isIpv6(); //return false
$ipfuture->isIpFuture(); //return true
$ipfuture->getIpVersion(); //return '32'
$domain = new Host('thephpleague.com'):
$domain->isIp(); //return false
$domain->isIpv4(); //return false
$domain->isIpv6(); //return false
$domain->isIpFuture(); //return false
$domain->getIpVersion(); //return null
~~~
### Zone Identifier
#### Detecting the presence of the Zone Identifier
The object can also detect if the IPv6 has a zone identifier or not. This can be handy if you want to know if you need to remove it or not for security reason.
~~~php
hasZoneIdentifier(); //return true
$ipv4 = new Host('127.0.0.1');
$ipv4->hasZoneIdentifier(); //return false
~~~
#### Removing the Zone Identifier
According to [RFC6874](http://tools.ietf.org/html/rfc6874#section-4):
> You **must** remove any ZoneID attached to an outgoing URI, as it has only local significance at the sending host.
To fullfill this requirement, the `Host::withoutZoneIdentifier` method is provided. The method takes not parameter and return a new host instance without its zone identifier. If the host has not zone identifier, the current instance is returned unchanged.
~~~php
withoutZoneIdentifier();
echo $newHost; //displays '[fe80::1]';
~~~
### Getting the IP string representation
You can retrieve the IP string representation from the Host object using the `getIp` method. If the Host is not an IP `null` will be returned instead.
~~~php
getIp(); //returns 'fe80::1%eth0-1'
$newHost = $host->withContent('uri.thephpleague.com');
$newHost->getIp(); //returns null
$newHost->getIpVersion(); //returns null
~~~
## Host represented by a registered name
If you don't have a IP then you are dealing with a registered name. A registered name can be a [domain name](http://tools.ietf.org/html/rfc1034) subset if it follows [RFC1123](http://tools.ietf.org/html/rfc1123#section-2.1) but it is not a requirement as stated in [RFC3986](https://tools.ietf.org/html/rfc3986#section-3.2.2)
> (...) URI producers should use names that conform to the DNS syntax, even when use of DNS is not immediately apparent, and should limit these names to no more than 255 characters in length.
~~~php
isDomain(); //return true
$reg_name = new Host('...test.com');
$reg_name->isDomain(); //return false
~~~
## Host represented by a domain name
If you don't have an IP or a general registered name it means you are using a domain name. As such the following method can be used to further caracterize your host.
~~~php
getPublicSuffix(); //display 'co.uk'
echo $host->getRegistrableDomain(); //display 'example.co.uk'
echo $host->getSubDomain(); //display 'www'
$host->isPublicSuffixValid(); //return a boolean 'true' in this example
~~~
If the data is not found the methods listed above will all return an **empty string** except for the `Host::isPublicSuffixValid` method which will return `false`.
~~~php
getPublicSuffix(); //return ''
echo $host->getRegistrableDomain(); //return ''
echo $host->getSubDomain(); //return ''
$host->isPublicSuffixValid(); //return false
~~~
### Updating the Registrable domain part
You can update the registrable domain part of the host.
~~~php
withRegistrableDomain('co.uk');
echo $newHost; //displays 'www.11.co.uk'
~~~
### Update the Host subdomains
You can update the subdomain part of the host.
~~~php
withSubDomain('shop');
echo $newHost; //displays 'shop.11.be'
~~~
### Host::createFromLabels
A host is a collection of labels delimited by the host separator `.`. So it is possible to create a `Host` object using a collection of labels with the `Host::createFromLabels` method.
The method expects at most 3 arguments:
- The first required argument must be a collection of label (an `array` or a `Traversable` object). **The labels must be ordered hierarchically, this mean that the array should have the top-level domain in its first entry**.
- The second optional argument, a `Host` constant, tells whether this is an FQDN or not:
- `Host::IS_ABSOLUTE` creates an a fully qualified domain name `Host` object;
- `Host::IS_RELATIVE` creates an a partially qualified domain name `Host` object;
By default this optional argument equals to `Host::IS_RELATIVE`.
- The third optional argument is a `League\Uri\PublicSuffix\Rules` object which will be used to resolved the host public suffix. If none is provided the Host will try to generate one using the defaut value from the [Hostname Parser component](/domain-parser/1.0/).
~~~php
root label, its string representation ends with a `.`, otherwise it is known as being a relative or a partially qualified domain name (PQDN).
~~~php
isIp(); //return false
$host->isAbsolute(); //return false
$fqdn = new Host('example.com.');
$fqdn->isIp(); //return false
$fqdn->isAbsolute(); //return true
$ip = new Host('[::1]');
$ip->isIp(); //return true
$ip->isAbsolute(); //return false
~~~
#### Updating the host status
To update the host state from FDQN to a PQDN and vice-versa you can use 2 methods
- `withRootLabel`
- `withoutRootLabel`
These methods which takes not argument add or remove the root empty label from the host as see below:
~~~php
withRootLabel() //display 'www.11.be.'
echo $host->withoutRootLabel() //display 'www.11.be'
~~~
### Normalization
Whenever you create a new host your submitted data is normalized using non desctructive operations:
- the host is lowercased;
- the host is converted to its ascii representation;
~~~php
If the host is an IP, the array contains only one entry, the full IP.
~~~php
getLabels(); //return ['com', 'example', 'secure'];
$fqdn = new Host('secure.example.com.');
$arr = $fqdn->getLabels(); //return ['com', 'example', 'secure'];
$host = new Host('[::1]');
$arr = $host->getLabels(); //return ['::1'];
~~~
The class also implements PHP's `Countable` and `IteratorAggregate` interfaces. This means that you can count the number of labels and use the `foreach` construct to iterate over them.
~~~php
$label) {
echo $labels; //will display "com", then "example" and last "secure"
}
~~~
#### Accessing Host label offset
If you are interested in getting the label offsets you can do so using the `Host::keys` method.
~~~php
keys(); //return [0, 1, 2, 3];
$host->keys('uk'); //return [0, 3];
$host->keys('gweta'); //return [];
~~~
The method returns all the label keys, but if you supply an argument, only the keys whose label value equals the argument are returned.
#### Accessing Host label value
If you are only interested in a given label you can access it directly using the `Host::getLabel` method as show below:
~~~php
getLabel(0); //return 'uk'
$host->getLabel(23); //return null
$host->getLabel(23, 'now'); //return 'now'
~~~
If the offset does not exists it will return the value specified by the optional second argument or default to `null`.
~~~php
getLabel(-1); //return 'uk'
$host->getLabel(-23); //return null
$host->getLabel(-23, 'now'); //return 'now'
~~~
### Manipulating the host labels
#### Appending labels
To append labels to the current host you need to use the `Host::append` method. This method accepts a single argument which represents the data to be appended. This data can be a string or `null`.
~~~php
append('toto')->append('example.com');
echo $newHost; //return toto.example.com
~~~
#### Prepending labels
To prepend labels to the current host you need to use the `Host::prepend` method. This method accept a single argument which represents the data to be prepended. This data can be a string or `null`.
~~~php
prepend('example.com')->prepend('toto');
echo $newHost; //return toto.example.com
~~~
#### Replacing labels
To replace a label you must use the `Host::replaceLabel` method with two arguments:
- The label's key to replace if it exists **MUST BE** an integer.
- The data to replace the key with. This data must be a string or `null`.
~~~php
replaceLabel(2, 'bar.baz');
echo $newHost; //return bar.baz.example.com
~~~
#### Removing labels
To remove labels from the current object you can use the `Host::withoutLabels` method. This method expects a single argument and will returns a new `Host` object without the selected labels. The argument is an array containing a list of offsets to remove.
~~~php
withoutLabels([1]);
$newHost->__toString(); //return toto.com
~~~
#### Modifying the Resolver object
At any given time you may change the `League\Uri\PublicSuffix\Rules` used to resolve the host public suffix using the following method
~~~php