Download the PHP package kuria/url without Composer
On this page you can find all versions of the php package kuria/url. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package url
Url ###
Parsing, modifying and building URLs.
:depth: 3
Features
- parsing URLs
- building relative and absolute URLs, including protocol-relative URLs
- getting, checking and setting individual URL components:
- scheme
- host
- port
- path
- query parameters
- fragment
Requirements
- PHP 7.1+
Usage
Creating a new URL
Create a new instance of Url
and use constructor arguments or setters
to define the components:
$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
$url = Url::parse('http://example.com:8080/test?foo=bar&lorem=ipsum#fragment');
Getting URL components
// checking whether a certain component is defined var_dump( $url->hasScheme(), $url->hasHost(), $url->hasPort(), $url->hasPath(), $url->hasQuery(), $url->hasFragment() );
Output:
string(4) "http"
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)
Getting query parameters
$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
Removing a single parameter
Setting multiple parameters
Replacing all parameters
Removing all parameters
Building URLs
Using build()
or __toString()
These methods will return an absolute or relative URL.
- if no host is specified, a relative URL will be returned
- if the host is specified, an absolute URL will be returned (unless the preferred format option is set to relative) $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"
Specifying a preferred format
By default, build()
and __toString()
return an absolute URL if the host is specified.
This behavior can be changed by passing the $preferredFormat
parameter to the constructor,
Url::parse()
or the setPreferredFormat()
method.
Url::RELATIVE
- prefer generating a relative URL even if the host is specifiedUrl::ABSOLUTE
- prefer generating an absolute URL if a host is specified $url = Url::parse('http://example.com/foo'); // print URL using the default preferred format (absolute) echo $url, "\n"; // set the preferred format to relative $url->setPreferredFormat(Url::RELATIVE); echo $url, "\n";
Output:
http://example.com/foo
/foo
Using buildAbsolute()
This method will always return an absolute URL.
If the host is not defined, Kuria\Url\Exception\IncompleteUrlException
will be thrown.
$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.
$url = new Url();
$url->setScheme('http'); $url->setHost('example.com'); $url->setPath('/test');
var_dump($url->buildRelative());
Output:
string(5) "/test"