The League of Extraordinary Packages

Our Packages:

Presented by The League of Extraordinary Packages

Getting Started

Uri

Uri Schemes

Uri manipulation

Uri Parts API

Services

Upgrading Guide

URI Handling

URI instantiation

To ease URI instantiation, and because URIs come in different forms we used named constructors to offer several ways to instantiate the object.

If a new instance can not be created an InvalidArgumentException exception is thrown

From a string

<?php

public static Uri::createFromString(string $uri = ''): Uri

Using the createFromString static method you can instantiate a new URI object from a string or from any object that implements the __toString method. Internally, the string will be parsed using the library internal URI parser.

<?php

use League\Uri\Schemes\Ftp as FtpUri;

$uri = FtpUri::createFromString('ftp://host.example.com/path/to/image.png;type=i');

From parse_url results

<?php

public static Uri::createFromComponents(array $components = []): Uri

You can also instantiate a new URI object using the createFromComponents named constructor by giving it the result of PHP’s function parse_url or the library internal URI parser.

<?php

use League\Uri\Schemes\Ws as WsUri;

$components = parse_url('wss://foo.example.com/path/to/index.php?param=value');

$uri = WsUri::createFromComponents($components);
It is not recommend to instantiate an URI object using the default constructor. It is easier to always use the documentated named constructors since each URI object requires a specific set of URI components objects.

Generic URI Handling

Out of the box the library provides the following specialized classes:

But you can easily create your own class to manage others scheme specific URI.

URI normalization

Out of the box the package normalizes any given URI according to the non destructive rules of RFC3986.

These non destructives rules are:

<?php

use League\Uri\Schemes\Http as HttpUri;

$uri = HttpUri::createFromString("hTTp://www.ExAmPLE.com:80/hello/./wor ld?who=f 3#title");
echo $uri; //displays http://www.example.com/hello/./wor%20ld?who=f%203#title