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:
null;- a valid component string for the specified URL component;
- an object implementing the
__toStringmethod;
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)
Return true if both components string representation values are equals.
Single Value Components
The URL components classes which represent single values only:
- implement the
League\Url\Components\ComponentInterfaceinterface. - differ in the way they validate and/or output the components.
These classes are:
League\Url\Components\Schemefor the scheme component;League\Url\Components\Userfor the user component;League\Url\Components\Passfor the pass component;League\Url\Components\Portfor the port component;League\Url\Components\Fragmentfor the fragment component;
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:
CountableIteratorAggregateArrayAccessLeague\Url\Components\ComponentArrayInterface
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.
The URL components classes implementing these interfaces are:
- League\Url\Components\Query for the query component;
- League\Url\Components\Path for the path component;
- League\Url\Components\Host for the host component;
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.
- The
$whenceargument specify the string segment where to include the data; - The
$whence_indexargument specify the$whenceindex if it is present more than once. The value starts at0;
SegmentInterface::prepend($data, $whence = null, $whence_index = null)
Prepends data into the component;
- The
$whenceargument specify the string segment where to include the data; - The
$whence_indexargument specify the$whenceindex if it is present more than once. The value starts at0;
SegmentInterface::remove($data)
Removes data from the component. If the pattern is present multiple times only the first match found is removed.