PSR interoperability

As we are dealing with URI, the package provides classes compliant with PSR-7 and PSR-17. This is done to allow more interoperability amongs PHP packages.

PSR-7 compatibility

The Http class implements the PSR-7 UriInterface interface. This means that you can use this class anytime you need a PSR-7 compliant URI object. Behind the scene, the implementation uses the Uri object and thus presents the same features around URI validation, modification and normalization.

The class handles all URI schemes BUT default to HTTP(s) rules if the scheme is not present and not recognized as special.

While the default constructor is private and can not be accessed to instantiate a new object, the League\Uri\Http class comes with named constructors to ease instantiation.

The following examples show how to use the different named constructors:


use League\Uri\Http;
use League\Uri\UriString;
use Laminas\Diactoros\Uri as LaminasUri;

// using a string or an object which expose the `__toString` method

$uri = Http::new('');
echo $uri; // display ''

$laminasUri = new LaminasUri("");
$laminasUri->getQuery(); //return '';

Http::new($laminasUri)->getQuery(); //return '';

// using `parse_url` or the package `UriString::parse` static method.

$uri = Http::fromComponents(UriString::parse("http://uri.thephpleague/7.0/uri/api"));

//don't forget to provide the $_SERVER array
$uri = Http::fromServer($_SERVER);

If you supply your own hash to fromComponents, you are responsible for providing well parsed components without their URI delimiters.

The fromServer method only relies on the server's safe parameters to determine the current URI. If you are using the library behind a proxy the result may differ from your expectation as no $_SERVER['HTTP_X_*'] header is taken into account for security reasons.

You can also return a URI based on standard specifications:


use League\Uri\Http;

$uri = Http::fromBaseUri("./p#~toto", "");
echo $uri; //displays ""

$template = '{hotel}/bookings/{booking}';
$variables = ['booking' => '42', 'hotel' => 'Rest & Relax'];
echo Http::fromTemplate($template, $variables)->toString();
//displays ""

The fromBaseUri method resolves URI using the same logic behind URL construction in a browser and is inline with how the Javascript URL object constructor works. If no base URI is provided, the URI to resolve MUST be absolute. Otherwise, the base URI MUST be absolute.

The fromTemplate method resolves a URI using the rules and variable from the URITemplate specification RFC6570: The method expects at most two parameters. The URI template to resolve and the variables use for resolution. You can get a more in-depth understanding of URI Template in its dedicated section of the documentation.

In addition to PSR-7 compliance, the class implements PHP’s JsonSerializable interface.

use League\Uri\Http;

echo json_encode(Http::new(''));
// display "http:\/\/\/path\/to?q=foo%20bar#section-42"

PSR-17 compatibility

The package also provides an implementation of the UriFactoryInterface from PSR-17

use League\Uri\HttpFactory;

$uriFactory = new HttpFactory();
$uri = $uriFactory->createUri('');
echo $uri::class; // display League\Uri\Http