The League of Extraordinary Packages

Our Packages:

Presented by The League of Extraordinary Packages

Getting Started

The API

The Components

Url is end of life. We strongly encourage you to use League\Uri instead.

URL components

An URL string is composed of up to 8 components. The League\Url library provides interfaces and classes to interact with each URL component. The classes can all be use independently of a League\Url\UrlInterface implementing class.

Component Interface

Each component class implements the League\Url\Components\ComponentInterface with the following public methods:

ComponentInterface::set($data)

Sets the component value.

The $data argument can be:

ComponentInterface::get()

Returns null if the class data is empty or its string representation

ComponentInterface::__toString()

Returns a typecast string representation of the component.

ComponentInterface::getUriComponent()

Returns an altered string representation to ease URL representation.

ComponentInterface::sameValueAs(ComponentInterface $component)

Added in version 3.2

Return true if both components string representation values are equals.

Single Value Components

The URL components classes which represent single values only:

These classes are:

Example using the League\Url\Components\Scheme class:

<?php

use League\Url\Components\Scheme;

$scheme = new Scheme;
$scheme->get(); //will return null since no scheme was set
echo $scheme; // will echo '' an empty string
echo $scheme->getUriComponent(); //will echo '//'
$scheme->set('https');
echo $scheme->__toString(); //will echo 'https'
echo $scheme->getUriComponent(); //will echo 'https://'

Multiple Values Components

In addition to the League\Url\Components\ComponentInterface, classes that deal with multiple values components implement the following interfaces:

The League\Url\Components\ComponentArrayInterface adds the following methods:

ComponentArrayInterface::toArray()

Returns an array representation of the component;

ComponentArrayInterface::keys()

Returns all the keys or a subset of the keys of an array if a value is given.

Of note: The $data argument for the set method can also be an array or a Traversable object.

The URL components classes implementing these interfaces are:

Segment Values Components

League\Url\Components\Path and League\Url\Components\Host also implement the League\Url\Components\SegmentInterface interface which adds the following methods:

The $data argument used in all described method below can be null, a valid component string, a object implementing the __toString method, an array or a Traversable object;

SegmentInterface::append($data, $whence = null, $whence_index = null)

Appends data into the component.

SegmentInterface::prepend($data, $whence = null, $whence_index = null)

Prepends data into the component;

Tips: You can easily get the $whence_index by using the object keys($whence) method result.

SegmentInterface::remove($data)

Removes data from the component. If the pattern is present multiple times only the first match found is removed.