The League of Extraordinary Packages

Our Packages:

Presented by The League of Extraordinary Packages

URI

Modern API to process URI in PHP.

$ composer require league/uri

Releases

Current Stable

URI 5.0

  • Requires: PHP >= 7.0.0
  • Latest: 5.0.0 - 2017-02-06
  • Supported Until: TBD

Full Documentation

Once a new major version is released, the previous stable release remains supported for six more months through patches and security fixes.

Highlights

The library provides simple and intuitive classes to parse, validate, format and manipulate URIs and their components. It is built to enable working with any kind of RFC3986 compliant URI through extensions and middlewares.

  1. Simple and extensible API

  2. RFC3986 compliant

  3. Implements PSR-7 UriInterface interface

  4. Framework-agnostic

Features

Parsing URI

The League URI Parser parses any given URI according to RFC3986 rules

<?php

use League\Uri\Parser;

$parser = new Parser();
var_export($parser('http://foo.com?@bar.com/'));
//returns the following array
//array(
//  'scheme' => 'http',
//  'user' => null,
//  'pass' => null,
//  'host' => 'foo.com',
//  'port' => null,
//  'path' => '',
//  'query' => '@bar.com/',
//  'fragment' => null,
//);

Normalizing URI

League URI objects normalize the URI string according to RFC3986/RFC3987 non destructive rules.

<?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

$uri = HttpUri::createFromComponent(parse_url("hTTp://www.bébé.be?#"));
echo $uri; //displays http://xn--bb-bjab.be?#

URI Middlewares

A collection of URI middlewares to enable reliable URI modifications with any given League URI object or PSR-7 UriInterface compatible implementation.

<?php

use League\Uri\Modifiers\MergeQuery;
use Zend\Diactoros\Uri;

$base_uri = "http://www.example.com?fo.o=toto#~typo";
$query_to_merge = 'fo.o=bar&taz=';

$uri = new Uri($base_uri);
$modifier = new MergeQuery($query_to_merge);

$new_uri = $modifier->process($uri);
echo $new_uri;
// display http://www.example.com?fo.o=bar&taz=#~typo
// $new_uri is a Zend\Diactoros\Uri instance

URI components

Improve URI components manipulations through dedicated objects.

<?php

use League\Uri\Components\Host;

$host = new Host('www.example.co.uk');
echo $host->getPublicSuffix();        //display 'co.uk'
echo $host->getRegisterableDomain();  //display 'example.co.uk'
echo $host->getSubdomain();           //display 'www'
$host->isPublicSuffixValid();         //return a boolean 'true' in this example

Questions?

URI was created by Ignace Nyamagana Butera. Find him on Twitter at @nyamsprod.