The League of Extraordinary Packages

Our Packages:

Presented by The League of Extraordinary Packages

Getting Started

Uri parser

Uri schemes

Uri manipulations

Uri components

Upgrading Guide

Components common API

Creating new URI component object

All URI object can be instantiate using the default constructor by providing:

public Component::__construct($content = null): void

the $content argument can be null, a string or an integer (in case of the Port object).

submitted string is normalized to be RFC3986 compliant.

If the submitted value is not valid a League\Uri\Components\Exception exception is thrown.

The League\Uri\Components\Exception extends PHP’s SPL InvalidArgumentException.

Accessing URI component representation

Once instantiated, all URI component objects expose the methods and constant defined in the ComponentInterface interface. This interface is used to normalized URI component representation while taking into account each component specificity.


const ComponentInterface::NO_ENCODING = 0;
const ComponentInterface::RFC1738_ENCODING = 1;
const ComponentInterface::RFC3986_ENCODING = 2;
const ComponentInterface::RFC3987_ENCODING = 3;
public ComponentInterface::isNull(void): bool
public ComponentInterface::isEmpty(void): bool
public ComponentInterface::getContent(int $enc_type = self::RFC3986_ENCODING): mixed
public ComponentInterface::__toString(void): string
public ComponentInterface::getUriComponent(void): string

Which will lead to the following results:


use League\Uri\Components\{

$scheme = new Scheme('HtTp');
echo $scheme->__toString(); //displays 'http'
echo $scheme->getUriComponent(); //display 'http:'

$userinfo = new UserInfo('john');
echo $userinfo->__toString(); //displays 'john'
echo $userinfo->getUriComponent(); //displays 'john@'

$path = new HierarchicalPath('/toto le heros/file.xml');
echo $path->getContent(HierarchicalPath::NO_ENCODING); //displays '/toto le heros/file.xml'
echo $path->__toString(); //displays '/toto%20le%20heros/file.xml'

$host = new Host('bébé.be');
echo $host; //displays ''
echo $host->getContent(Host::RFC3987_ENCODING); //displays 'bébé.be'

$query = new Query();
$query->isNull(); //returns true
$query->isEmpty(); //returns true
echo $query->getContent(); //displays null

$path = new Path('');
$path->isNull(); //returns false
$path->isEmpty(); //returns true
echo $path->getContent(); //displays ''

$port = new Port(23);
$port->isNull(); //returns false
$port->isEmpty(); //returns false
echo $port->getContent(); //displays (int) 23;

For a more generalized representation you must use the ComponentInterface::getContent method. This method returns type can be:

The string is normalized and encoded according to the component rules and the optional $enc_type parameter. The $enc_type parameter can take on of the following values:

Normalization and encoding are component specific.

By default, $enc_type equals ComponentInterface::RFC3986_ENCODING

Modifying URI component object

All URI component objects can be modified with the ComponentInterface::withContent method. This method accepts:

and returns a new instance with the modified content.

submitted string is normalized to be RFC3986 compliant.


use League\Uri\Components\{

$query = new Query();
$new_query = $query->withContent('');
echo $query->getContent(); //returns null
echo $new_query->getContent(); //returns ''

$host = new Host('');
$new_host = $host->withContent('bébé.be');
echo $new_host->getContent(); //displays '';
echo $new_host->getContent(Host::RFC3987_ENCODING); //displays 'bébé.be';

If the submitted value is not valid an League\Uri\Components\Exception exception is thrown.

List of URI component objects

The following URI component objects are defined (order alphabetically):

Some URI component objects expose more methods to enable better manipulations.