This Uri
class represents a URI (Uniform Resource Identifier) and provides methods to manipulate its components.
Creating a new object
use Mk4U\Http\Uri;
$uri=new Uri();
// or
$uri=new Uri('http://john:xyz%2A12@example.org:8080/en/download?name=param#footer');
The __toString
magic method returns the URI converted to a string.
echo $uri;
// return "http://john:xyz%2A12@example.org:8080/en/download?name=param#footer"
The __debugInfo
magic method returns the URI representation as an array for debugging purposes.
var_dump($uri);
/* return [
"schema"=> "http",
"user-info"=> "john:xyz%2A12",
"host"=> "example.org",
"port"=> 8080,
"path"=> "/en/download",
"query"=> "name=param",
"fragment"=> "footer",
]*/
This method is in charge of setting the URL scheme
Parameters:
$scheme
(string): The scheme you want to set for the URL.
$uri->setScheme($scheme);
This method sets the user information in the URI to the format "user:password" if the password is provided. If the password is not provided, only the username is set.
Parameters:
$user
(string): The user name to be used to obtain authority.$password
(string|null): The password associated with the user. It is optional.
$uri->setUserInfo($user, $password);
This method is responsible for setting the host of the URL.
Parameters:
$host
(string): The host to be set for the URL.
$uri->setHost($host);
This method is responsible for setting the port of the URL. If the port is not valid, it throws an InvalidArgumentException
.
Parameters:
$port
(string): The port to be set for the URL.
$uri->setPort($port);
This method is responsible for setting the URL path.
Parameters:
$path
(string): The path to set for the URL.
$uri->setPath($path);
This method is responsible for setting the URL queries.
Parameters:
$query
(string): The queries you want to set for the URL.
$uri->setQuery($query);
This method takes care of setting the URL fragment.
Parameters:
$fragment
(string): The fragment you want to set for the URL.
$uri->setFragment($fragment);
This method retrieves the schema component of the URI.
$uri->getScheme();
// return "http"
This method retrieves the host component of the URI.
$uri->getHost();
// return "example.org"
This method retrieves the authority component of the URI.
$uri->getAuthority();
This method retrieves the user information component of the URI.
$uri->getUserInfo();
// return "john:xyz%2A12"
This method retrieves the port component of the URI.
It checks if the schema is empty and if the port is the default port for that schema. Returns the port if it is not the default port, otherwise returns null.
$uri->getPort();
// return 8080
This method retrieves the path component of the URI.
$uri->getPath();
// return "/en/download"
This method retrieves the query string of the URI.
Parameters:
$array
(bool): optional. If set to true, the query is returned as an array.
$uri->getQuery();
// return "name=param"
$uri->getQuery(true);
// return ["name"=>"param"]
This method retrieves the fragment component of the URI.
$uri->getFragment();
// return "footer"