The League of Extraordinary Packages

Our Packages:

Presented by The League of Extraordinary Packages

Getting Started


Uri Schemes

Uri manipulation

Uri Parts API


Upgrading Guide

This is the documentation for the unsupported version 4.0. Please consider upgrading your code to the latest stable version

URI parts and components

An URI string is composed of 8 components and 5 parts:

 \_/   \______________/\_________/ \_________/ \__/
  |           |            |            |        |
scheme     authority       path        query   fragment
  |   _____________________|__
 / \ /                        \

The URI authority part in itself can be composed of up to 3 parts.
\______/ \_________/ \__/
    |         |        |
userinfo    host     port

The userinfo part is composed of the user and the pass components.

\_____/ \____/
   |      |
  user   pass

The League\Uri package uses two interfaces as fundation to implement any URI component or part.

URI part interface


namespace League\Uri\Interfaces;

interface UriPart
    public function __toString(void);
    public function getUriComponent(void);
    public function sameValueAs(UriPart $component);

The UriPart interface exposes methods that allow a basic representation of an URI part.


Returns the normalized and encoded string version of the URI part. This is the form used when echoing the URI component from the URI object getter methods.


public UriPart::__toString(void): string



use League\Uri\Components\Scheme;
use League\Uri\Components\UserInfo;
use League\Uri\Components\HierarchicalPath;

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

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

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

Normalization and encoding are specific to the URI part.


Returns the string representation of the normalized and encoded URI part with its optional delimiter if required. This is the form used by the URI object __toString method when building the URI string representation.


public UriPart::getUriComponent(void): string



use League\Uri\Components\Scheme;

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

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

Normalization, encoding and delimiters are specific to the URI part.


New in version 4.2

Returns the normalized and encoded version of the URI part.


public UriPart::getContent(void): mixed

This method return type is:



use League\Uri\Components\Query;
use League\Uri\Components\Port;

$component = new Query();
echo $component->getContent(); //displays null

$component = new Query('');
echo $component->getContent(); //displays ''

$component = new Port(23);
echo $component->getContent(); //displays (int) 23;


To avoid BC Break getContent is not part of the UriPart interface but will be added in the next major release.

Differences between UriPart representations

To understand the differences between the described representations see the examples below:


use League\Uri\Components\Fragment;

$component = new Fragment('');
$component->getContent(); //returns ''
echo $component; //displays ''
echo $component->getUriComponent(); //displays '#'

$altComponent = new Fragment(null);
$altComponent->getContent(); //returns null
echo $component; //displays ''
echo $altComponent->getUriComponent(); //displays ''

In both cases, the __toString returns the same value but the other methods do not.

The __toString method is unabled to distinguish between an empty and an undefined URI part.


Since version 4.2 this method is deprecated.

Compares two UriPart object to determine whether they are equal or not. The comparison is based on the result of UriPart::getUriComponent from both objects.


public UriPart::sameValueAs(UriPart $component): bool



use League\Uri\Components\Host;
use League\Uri\Components\Fragment;
use League\Uri\Schemes\Http as HttpUri;

$host     = new Host('');
$alt_host = new Host('');
$fragment = new Fragment('');
$uri      = HttpUri::createFromString('');

$host->sameValueAs($alt_host); //return true;
$host->sameValueAs($fragment); //return false;
//a PHP Fatal Error is issue or a PHP7+ TypeError is thrown

Only Uri parts objects can be compared, any other object will result in a PHP Fatal Error or a PHP7+ TypeError will be thrown.

UriPart implementing classes

URI component interface

This interface which extends the UriPart interface is only implemented by URI components classes.


namespace League\Uri\Interfaces;

use League\Uri\Interfaces\UriPart;

interface Component extends UriPart
    public function modify($value);


Creates a new Component object with a modified content. The original object is not modified.


public Component::modify($value): Component



use League\Uri\Components\Query;

$query = new Query('q=url&site=thephpleague');
$new_query = $query->modify('q=yolo');
echo $new_query; //displays 'q=yolo'
echo $query;     //display 'q=url&site=thephpleague'


New in version 4.2


All objects implements PHP5.6+ __debugInfo magic method in order to help developpers debug their code. The method is called by var_dump and displays the Uri components string representation.


use League\Uri\Schemes\Host;

$host = new Host("");

//displays something like
// object(League\Uri\Components\Host)#1 (1) {
//     ["host"]=> string(11) ""
// }


For the same purpose of debugging and object exportations PHP’s magic method __set_state is also supported


use League\Uri\Schemes\Host;

$host = new Host("");
$newHost = eval('return '.var_export($host, true).';');

$host->__toString() == $newHost->__toString();

Component implementing classes