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

Extracting data from URIs

Even thought the package comes bundle with a serie of URI objects representing usual URI, all theses objects expose the same methods.

Accessing URI parts and components as strings

You can access the URI string, its individual parts and components using their respective getter methods.

<?php

public Uri::__toString(): string
public Uri::getScheme(void): string
public Uri::getUserInfo(void): string
public Uri::getHost(void): string
public Uri::getPort(void): int|null
public Uri::getAuthority(void): string
public Uri::getPath(void): string
public Uri::getQuery(void): string
public Uri::getFragment(void): string

Which will lead to the following result for a simple URI:

<?php

use League\Uri\Schemes\Http as HttpUri;

$uri = HttpUri::createFromString("http://foo:bar@www.example.com:81/how/are/you?foo=baz#title");
echo $uri;                 //displays "http://foo:bar@www.example.com:81/how/are/you?foo=baz#title"
echo $uri->getScheme();    //displays "http"
echo $uri->getUserInfo();  //displays "foo:bar"
echo $uri->getHost();      //displays "www.example.com"
echo $uri->getPort();      //displays 81 as an integer
echo $uri->getAuthority(); //displays "foo:bar@www.example.com:81"
echo $uri->getPath();      //displays "/how/are/you"
echo $uri->getQuery();     //displays "foo=baz"
echo $uri->getFragment();  //displays "title"

Accessing URI parts and components as objects

To access a specific URI part or component as an object you can use PHP’s magic method __get as follow.

The __get method is not part of any interface.

<?php

use League\Uri\Schemes\Ws as WsUri;

$uri = WsUri::createFromString("http://foo:bar@www.example.com:81/how/are/you?foo=baz");
$uri->scheme;   //return a League\Uri\Components\Scheme object
$uri->userInfo; //return a League\Uri\Components\UserInfo object
$uri->host;     //return a League\Uri\Components\Host object
$uri->port;     //return a League\Uri\Components\Port object
$uri->path;     //return a League\Uri\Components\HierarchicalPath object
$uri->query;    //return a League\Uri\Components\Query object
$uri->fragment; //return a League\Uri\Components\Fragment object

Using this technique you can get even more informations regarding your URI.

<?php

use League\Uri\Schemes\Http as HttpUri;

$uri = HttpUri::createFromString("http://foo:bar@www.example.com:81/how/are/you?foo=baz");
$uri->host->isIp();           //return false the URI uses a registered hostname
$uri->userInfo->getUser();    //return "foo" the user login information
$uri->fragment->__toString(); //return '' because the fragment component is undefined
$uri->path->getBasename();    //return "you"
$uri->query->getValue("foo"); //return "baz"

The actual methods attach to each component depend on the underlying component object used. For instance a DataUri::path object does not expose the same methods as a Ws::path object would.

To get more informations about component properties refer to the components documentation

Getting URI object reference status

New in version 4.2

Description

<?php

function League\Uri\uri_reference(mixed $uri [, mixed $base_uri]): array

This function analyzes the submitted URI object and returns an associative array containing information regarding the URI-reference.

As per RFC3986 URI-reference is used to denote the most common usage of a resource identifier. The specification defines 5 possible types of references for any given URI.

Parameters

Returns Values

An associative array is returned. The following keys are always present within the array and their content is always a boolean:

Examples

<?php

use League\Uri\Schemes\Http as HttpUri;
$uri = HttpUri::createFromString("//스타벅스코리아.com/how/are/you?foo=baz");
$alt_uri = HttpUri::createFromString("//xn--oy2b35ckwhba574atvuzkc.com/how/are/you?foo=baz#bar");

var_dump(League\Uri\uri_reference($uri));
//displays something like
// array(5) {
//   'absolute_uri' => bool(false)
//   'network_path' => bool(true)
//   'absolute_path' => bool(false)
//   'relative_path' => bool(false)
//   'same_document' => bool(false)
// }

var_dump(League\Uri\uri_reference($uri, $alt_uri));
//displays something like
// array(5) {
//   'absolute_uri' => bool(false)
//   'network_path' => bool(true)
//   'absolute_path' => bool(false)
//   'relative_path' => bool(false)
//   'same_document' => bool(true)  //can be true only if a base URI is provided
// }

Debugging URI objects

New in version 4.2

__debugInfo

All Uri objects from the package implements PHP5.6+ __debugInfo magic method in order to help developpers debug their code. The method is called by var_dump and returns an array with an uri key whose value is the object string representation.

<?php

use League\Uri\Schemes\Http as HttpUri;

$uri = HttpUri::createFromString("http://example.com/path?query=value#fragment");

var_dump($uri);
//displays something like
// object(League\Uri\Schemes\Http)#11 (1) {
//     ["uri"]=> string(44) "http://example.com/path?query=value#fragment"
// }

__set_state

For the same purpose of debugging and object exportations PHP’s magic method __set_state is also supported

<?php

use League\Uri\Schemes\Http as HttpUri;

$uri = HttpUri::createFromString("http://foo:bar@www.example.com:81/how/are/you?foo=baz");
$newUri = eval('return '.var_export($uri, true).';');

$uri->__toString() == $newUri->__toString();