PSR-7 URI factory

2.2 2020-05-31 07:34 UTC

This package is auto-updated.

Last update: 2021-11-29 02:17:54 UTC


Latest Stable Version License Build Status Coverage Status Quality Score Total Downloads

Uri Factory

A library / framework agnostic PSR-7 UriInterface factory.

There are several PSR-7 libraries on packagist but each one has its own factory for creating an UriInterface object from a string.

bentools/uri-factory has no explicit dependency and automatically picks up your favorite library for creating UriInterface instances.

Supported libraries so far:

  • guzzlehttp/psr7
  • zendframework/zend-diactoros
  • league/uri
  • nyholm/psr7
  • ringcentral/psr7


On any URL string:

use function BenTools\UriFactory\Helper\uri;
$uri = uri(''); // Will convert to a Psr\Http\Message\UriInterface instance

On current location:

use function BenTools\UriFactory\Helper\current_location;
$uri = current_location(); // Will convert the current location to a Psr\Http\Message\UriInterface instance

You can specify which library to use, by using the corresponding adapter:

use BenTools\UriFactory\Adapter\GuzzleAdapter;
use BenTools\UriFactory\Adapter\LeagueUriAdapter;
use BenTools\UriFactory\Adapter\ZendDiactorosAdapter;
use function BenTools\UriFactory\Helper\current_location;
use function BenTools\UriFactory\Helper\uri;

$uri = uri('', GuzzleAdapter::factory());
$uri = uri('', LeagueUriAdapter::factory());
$uri = uri('', ZendDiactorosAdapter::factory());

$uri = current_location(GuzzleAdapter::factory());
$uri = current_location(LeagueUriAdapter::factory());
$uri = current_location(ZendDiactorosAdapter::factory());


This library ships with an URL canonicalizer.

It is not a perfect one since your PSR-7 library may behave differently regarding special chars, but it should work most of the time.

The canonicalize() function accepts any PSR-7 UriInterface object and will return a canonicalized one.

require_once __DIR__ . '/vendor/autoload.php';

use function BenTools\UriFactory\Helper\canonicalize;
use function BenTools\UriFactory\Helper\uri;

$url = '';
echo canonicalize(uri($url)); //


PHP 7.1+ is required.

composer require bentools/uri-factory




If you want bentools/uri-factory to support more PSR-7 libraries, feel free to submit a PR.