A simple and useful URI package for Laravel framework.
This package provides an object representation of a uniform resource identifier (URI), an easy access and manipulation to the parts of the URI.
This package requires PHP 7.2 and Laravel 5.8 or higher.
Install with composer:
composer require nsaliu/laravel-uri
Creates a new instance with the specified URI.
$uri = new Uri();
$uri->createFromString('https://test.test');
Return the string representation of the URI instance.
$uri = new Uri();
$uri->createFromString('https://test.test');
$uri->toString();
Check with a GET call if the host returns an HTTP status code equals to 200.
$uri = new Uri();
$uri->createFromString('https://test.test');
$uri->hostIsReachable();
Check if a string representation of the URI equals to the URI instance.
$uri = new Uri();
$uri->createFromString('https://test.test');
$uri->equals('https://test.test');
Gets all the URIs components if presents.
$uri = new Uri();
$uri->createFromString('https://fakeuser:[email protected]:443/path1/path2/page.html?key1=value1&key2=value2#fragment');
$uri->getComponents();
Return:
[
'scheme' => 'https'
'host' => 'test.test'
'port' => 443
'user' => 'fakeuser'
'pass' => 'fakepass'
'path' => '/path1/path2/page.html'
'query' => 'key1=value1&key2=value2'
'fragment' => 'fragment'
]
Get the scheme part of the URI.
$uri = new Uri();
$uri->createFromString('https://test.test');
$uri->getScheme();
If a scheme is present return https
otherwise an empty string
.
Get the user part of the URI.
$uri = new Uri();
$uri->createFromString('https://fakeuser:[email protected]');
$uri->getUsername();
If a user is present returns fakeuser
, otherwise return an empty string
.
Get the pass part of the URI.
$uri = new Uri();
$uri->createFromString('https://fakeuser:[email protected]');
$uri->getPassword();
If a password is set return fakepass
, otherwise return an empty string
.
Get the authority part of the URI.
$uri = new Uri();
$uri->createFromString('https://fakeuser:[email protected]');
$uri->getAuthority();
Return:
- if no authority information is present, this method return an
empty string
. - if authority is set return it in form of:
jhon:[email protected]
. - if the port is a standard port for scheme, the port is not included, otherwise yes.
Example:
// Create a URI with 'https' scheme and '80' as port (not default for https)
$uri = new Uri();
$uri->createFromString('https://fakeuser:[email protected]');
$uri->getAuthority();
Return jhon:[email protected]:80
Get the authority part of the URI with the port too, even if it's the default for the scheme.
$uri = new Uri();
$uri->createFromString('https://fakeuser:[email protected]:443');
$uri->getAuthorityWithPort();
Return jhon:[email protected]:443
Get the user part of the URI.
$uri = new Uri();
$uri->createFromString('https://fakeuser:[email protected]:443');
$uri->getUserInfo();
Return:
- if no user information is present return an
empty string
. - if a user is present in the URI return
fakeuser
. - if a password is present it returns the password too separated by ':' from the username
fakeuser:fakepass
Get the host part of the URI.
$uri = new Uri();
$uri->createFromString('https://test.test');
$uri->getHost();
Return test.test
Get the port part of the URI.
$uri = new Uri();
$uri->createFromString('https://test.test:443');
$uri->getPort();
Return:
- if the port is present return the port as an integer:
443
. - if the port is not present, return
null
.
Get whether the port value of the URI is the default for the scheme.
$uri = new Uri();
$uri->createFromString('https://test.test:80');
$uri->isDefaultPort();
// return false because 80 isn't the default port for https scheme
Return:
true
if the port is default for the scheme.false
if the port isn't the default.
Get the path value of the URI.
$uri = new Uri();
$uri->createFromString('https://test.test/path1/path2/page.html');
$uri->getPath();
Return:
- if a path is present return
/path1/path2/page.html
. - if the path is not present return an
empty string
.
Get the path value of the URI as an array.
$uri = new Uri();
$uri->createFromString('https://test.test/path1/path2/page.html');
$uri->getPathAsArray();
Return:
- if the path is present return the path portion of the URI as an array:
[
'path1',
'path2',
'page.html',
]
- if path is not present return an
empty string
.
Get the query part of the URI.
$uri = new Uri();
$uri->createFromString('https://test.test/path1/path2/page.html?key1=value1&key2=value2');
$uri->getQuery();
Return:
- if the query part of the URI is present return
key1=value1&key2=value2
. - if the query part of the URI is not present return an
empty string
.
Get the value of a given query key.
$uri = new Uri();
$uri->createFromString('https://test.test/path1/path2/page.html?key1=value1&key2=value2');
$uri->getQueryValue('key1');
Return:
- if the key exists in query return:
value1
. - if the key not exists in query part return an
empty string
.
Get the query part of the URI as an array.
$uri = new Uri();
$uri->createFromString('https://test.test/path1/path2/page.html?key1=value1&key2=value2');
$uri->getQueryAsArray();
Return:
- if the query part is present return:
[
'key1' => 'value1',
'key2' => 'value2',
]
- if the query part not exists return an
empty array
.
Get the path and query part of the URI.
$uri = new Uri();
$uri->createFromString('https://test.test/path1/path2/page.html?key1=value1&key2=value2');
$uri->getPathAndQuery();
Return:
- if the path and query exists return
/path1/path2/page.html?key1=value1&key2=value2
. - if the path exists and query not exists return
/path1/path2/page.html
. - if the path not exists and query too return an
empty string
.
Get the fragment part of the URI.
$uri = new Uri();
$uri->createFromString('https://test.test/page.html?key1=value1&key2=value2#fragment-1');
$uri->getFragment();
Return:
- if the fragment is present return
fragment-1
. - if the fragment is not present return an
empty string
.
Set the scheme part of the URI.
$uri = new Uri();
$uri->setScheme('http');
Set the user part of the URI.
$uri = new Uri();
$uri->setUsername('username');
Set the password part of the URI.
$uri = new Uri();
$uri->setPassword('password');
Set the user and password part of the URI.
$uri = new Uri();
$uri->setUserInfo('username'); // without password
$uri->setUserInfo('username', 'password'); // or with password
Set the host part of the URI.
$uri = new Uri();
$uri->setHost('test.test');
Set the port part of the URI.
$uri = new Uri();
$uri->setPort(80);
Set the path part of the URI.
$uri = new Uri();
$uri->setPath('path1/path2/page.html');
Set the query part of the URI.
$uri = new Uri();
$uri->setQuery('key1&value1&key2=value2');
Set the query part of the URI with an array.
$uri = new Uri();
$uri->setQueryArray(['key1' => 'value1', 'key2' => 'value2']);
Add a query key and value to the URI.
$uri = new Uri();
$uri->addQuery('key3', 'value3');
Change a query part of the URI.
$uri = new Uri();
$uri->changeQuery('key1', 'new-value');
Set the fragment part of the URI.
$uri = new Uri();
$uri->setFragment('fragment-2');