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

Generic URI Modifiers

Here’s the documentation for the included URI modifiers which are modifying multiple URI components at once.

Normalize a URI

To help wil URI objects comparison, the League\Uri\Modifiers\Normalize URI modifier is introduce to normalize URI according to the following rules:

If you normalized two URI objects it become easier to compare them to determine if they are referring to the same resource:


use League\Uri\Modifiers\Normalize;
use League\Uri\Schemes\Http;

$uri = Http::createFromString("http://스타벅스코리아.com/to/the/sky/");
$altUri = Http::createFromString("");
$modifier = new Normalize();

$newUri    = $modifier->__invoke($uri);
$newAltUri = $modifier->__invoke($altUri);

var_dump($newUri->__toString() === $newAltUri->__toString()); //return true

Resolving a relative URI



public Resolve::__construct(mixed $uri)

The Resolve URI Modifier provides the mean for resolving an URI as a browser would for a relative URI. When performing URI resolution the returned URI is normalized according to RFC3986 rules. The uri to resolved must be another Uri object.


$uri must be a League\Uri\Interfaces\Uri or a Psr\Http\Message implemented object



use League\Uri\Schemes\Http;
use League\Uri\Modifiers\Resolve;

$baseUri     = Http::createFromString("");
$relativeUri = Http::createFromString("./p#~toto");
$modifier    = new Resolve($baseUri);
$newUri = $modifier->__invoke($relativeUri);
echo $newUri; //displays ""

Relativize an URI

New in version 4.2



public Relativize::__construct(mixed $uri)

The Relativize URI Modifier provides the mean to construct a relative URI that when resolved against the same URI yields the same given URI. This modifier does the inverse of the Resolve modifier. The uri to relativize must be another Uri object.


$uri must be a League\Uri\Interfaces\Uri or a Psr\Http\Message implemented object



use League\Uri\Schemes\Http;
use League\Uri\Modifiers\Relativize;
use League\Uri\Modifiers\Resolve;

require '/path/to/vendor/autoload.php';

$baseUri = Http::createFromString('');
$relativizer = new Relativize($baseUri);
$resolver = new Resolve($baseUri);
$uri = Http::createFromString('');
$relativeUri = $relativizer($uri);
echo $relativeUri; // display "/?foo=toto#~typo
echo $resolver($relativeUri); // display ''

To be sure that both operations yield the expected results both URI must be normalized.

Applying multiple modifiers to a single URI

Since all modifiers returns a URI object instance it is possible to chain them together. To ease this chaining the package comes bundle with the League\Uri\Modifiers\Pipeline class. The class uses the pipeline pattern to modify the URI by passing the results from one modifier to the next one.

The League\Uri\Modifiers\Pipeline uses two methods:

The Pipeline::process is an alias of Pipeline::__invoke.


use League\Uri\Modifiers\HostToAscii;
use League\Uri\Modifiers\KsortQuery;
use League\Uri\Modifiers\Pipeline;
use League\Uri\Modifiers\RemoveDotSegments;
use League\Uri\Schemes\Http;

$origUri = Http::createFromString("http://스타벅스코리아.com/to/the/sky/");
$origUri2 = Http::createFromString("");

$modifier = (new Pipeline())
	->pipe(new RemoveDotSegment())
	->pipe(new HostToAscii())
	->pipe(new KsortQuery());

$origUri1Alt = $modifier->process($origUri1);
$origUri2Alt = $modifier->process($origUri2);

echo $origUri1Alt; //display
echo $origUri2Alt; //display

The League\Uri\Modifiers\Pipeline is a URI modifier as well which can lead to advance modifications from you URI in a sane an normalized way.

This class is heavily influenced by the League\Pipeline package.