Parsing, modifying and building URLs
Switch branches/tags
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
src
tests
.gitattributes
.gitignore
.travis.yml
CHANGELOG.rst
LICENSE
README.rst
composer.json
phpunit.xml.dist

README.rst

Url

Parsing, modifying and building URLs.

https://travis-ci.com/kuria/url.svg?branch=master

Features

  • parsing URLs
  • determining the current URL from $_SERVER properties
  • building relative and absolute URLs, including protocol-relative URLs
  • getting, checking and setting individual URL components:
    • scheme
    • host
    • port
    • user
    • password
    • path
    • query parameters
    • fragment

Requirements

  • PHP 7.1+

Usage

Getting the current URL

The current URL is determined using $_SERVER properties.

<?php

use Kuria\Url\Url;

$url = Url::current();

echo $url;

Example output:

http://localhost/test.php

Note

If $_SERVER['HTTP_HOST'] is undefined, localhost will be used as the default. This can be changed using Url::setDefaultCurrentHost().

Creating a new URL

Create a new instance of Url and use constructor arguments or setters to define the components:

<?php

use Kuria\Url\Url;

$url = new Url();

$url->setScheme('http');
$url->setHost('example.com');
$url->setPath('/test');
// many more setters are available..

echo $url;

Output:

http://example.com/test

Parsing an URL

<?php

use Kuria\Url\Url;

$url = Url::parse('http://bob:123456@example.com:8080/test?foo=bar&lorem=ipsum#fragment');

Getting URL components

var_dump(
    $url->getScheme(),
    $url->getUser(),
    $url->getPassword(),
    $url->getHost(),
    $url->getFullHost(),
    $url->getPort(),
    $url->getPath(),
    $url->getQuery(),
    $url->getFragment()
);

// checking whether a certain component is defined
var_dump(
    $url->hasScheme(),
    $url->hasUser(),
    $url->hasPassword(),
    $url->hasHost(),
    $url->hasPort(),
    $url->hasPath(),
    $url->hasQuery(),
    $url->hasFragment()
);

Output:

string(4) "http"
string(3) "bob"
string(6) "123456"
string(11) "example.com"
string(16) "example.com:8080"
int(8080)
string(5) "/test"
array(2) {
  ["foo"]=>
  string(3) "bar"
  ["lorem"]=>
  string(5) "ipsum"
}
string(8) "fragment"
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)

Getting query parameters

<?php

use Kuria\Url\Url;

$url = Url::parse('/test?foo=bar&lorem%5B0%5D=ipsum&lorem%5B1%5D=dolor');

var_dump(
    $url->has('foo'),
    $url->has('nonexistent'),
    $url->get('foo'),
    $url->get('lorem'),
    $url->get('nonexistent')
);

Output:

bool(true)
bool(false)
string(3) "bar"
array(2) {
  [0]=>
  string(5) "ipsum"
  [1]=>
  string(5) "dolor"
}
NULL

Manipulating query parameters

Setting a single parameter

<?php

$url->set('parameter', 'value');

Removing a single parameter

<?php

$url->remove('foo');

Setting multiple parameters

<?php

$url->add(['foo' => 'bar', 'lorem' => 'ipsum']);

Replacing all parameters

<?php

$url->setQuery(['foo' => 'bar']);

Removing all parameters

<?php

$url->removeAll();

Building URLs

Note

Building an URL with undefined scheme will yield a protocol-relative URL.

Example: //localhost/test

Using build() or __toString()

These methods will return an absolute or relative URL, depending on whether the host is defined.

<?php

use Kuria\Url\Url;

$url = new Url();

$url->setPath('/test');

var_dump($url->build());

$url->setScheme('http');
$url->setHost('example.com');

var_dump($url->build());

Output:

string(5) "/test"
string(23) "http://example.com/test"

Using buildAbsolute()

This method will always return an absolute URL.

If the host is not defined, the current host and scheme (if not defined) will be used instead. See Getting the current URL

<?php

use Kuria\Url\Url;

$url = new Url();

$url->setScheme('http');
$url->setHost('example.com');
$url->setPath('/test');

var_dump($url->buildAbsolute());

Output:

string(23) "http://example.com/test"

Using buildRelative()

This method will always return a relative URL regardless of whether the host is defined or not.

<?php

use Kuria\Url\Url;

$url = new Url();

$url->setScheme('http');
$url->setHost('example.com');
$url->setPath('/test');

var_dump($url->buildRelative());

Output:

string(5) "/test"