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

URI Formatter

The Formatter class helps you format your URI according to your output.



public Formatter::setEncoding(int $format): void
public Formatter::setQuerySeparator(string $separator): void
public Formatter::preserveQuery(bool $status): void
public Formatter::preserveFragment(bool $status): void
public Formatter::__invoke(mixed $uri): string

This main method __invoke expects an object implementing one of the following interface:

and returns a string representation of the submitted object according to the settings you gave it using the remaining methods.

The returned string MAY no longer be a valid URI


By default the formatter encode each URI component using RFC3986 rules. You can change the component encoding algorithm by specifying one of the predefined constant:


If you want to generate an URI string compatible with HTML rules you need for instance to convert the & to its HTML entity &amp;. This setter enables you to change the query separator in the return URI string.

preserveQuery and preserveFragment

By default PSR-7 UriInterface does not preserve query and fragment delimiters in the string representation. If you want to keep them you need to specify this behaviour to the Formatter object. By default, the Formatter does not keep them too.



use League\Uri\Formatter;
use Zend\Diactoros\Uri as DiactorosUri;

$formatter = new Formatter();

$uri = new DiactorosUri('');
echo $formatter($uri);
//displays 'https://рф.ru:81?foo=ba r&amp;baz=bar#'