Joomla tip: Use the Joomla\Uri\Uri class to create a URL.
When building a url in the code, you can collect all the strings with concatenation of the form $url = $domain.'/index.php?option='.$option.'&view='.$view.'¶m1='.$value1; and for small strings, it can even be convenient. But if there are a lot of parameters or they require standardization/cleaning in the process, then not everything is so convenient and obvious. For example, there may be a leading slash in part of the url (a slash at the beginning of the url fragment) and the incoming domain for the request may also end in a slash, and so we get an incorrect url for the request with a double slash somewhere in the middle... For the tasks of standardization and uniformity of url retrieval, Joomla has the Joomla\Uri\Uri class. Previously, with Joomla 1.6 and older, it was called JUri. This class provides work with urls according to the RFC3986 standard and takes over the work of parsing URLs for spare parts or assembling URLs from spare parts)) Example: get a specific parameter from the url. use Joomla\Uri\Uri; $url = 'https://web-tolk.ru/dev/biblioteki?param=value'; $uri = new Uri($url); // output the line 'value' here echo $uri->getVar('param'); Yes, there is a native PHP function parse_url, you might say... But the Uri class ensures safe operation with UTF-8 in urls, including Cyrillic domains. In order not to write various checks yourself, you can use the features of the Joomla core. How to build required url in the code in Joomla Here, too, everything is simple: use Joomla\Uri\Uri; $uri = new Uri; $uri->setHost('web-tolk.ru'); $uri->setScheme('https'); // setPath() starts with leading slash $uri->setPath('/dev/biblioteki'); // GET-parameters can be passed as an array $vars = [ 'param1' => 'value1', 'param2' => 'value2', 'param3' => 'value3', ]; $uri->setQuery($vars); // echo our url like string echo $uri->toString(); The hierarchy of Uri classes in Joomla is designed so that getter methods are in the AbstractUri class, and setters are in the Uri class. The setters can be viewed in the file libraries/vendor/joomla/uri/src/Uri.php . We look at Hetaera in the file libraries/vendor/joomla/uri/src/AbstractUri.php. If you have PHPStorm, then it knows Joomla perfectly well and tells you everything you need. Well, you can give a link to the old documentation page, which is still mostly relevant, adjusted for the use of namespaces. Uri structure: foo://example.com:8042/over/there?name=ferret#nose \_/ \______________/\_________/ \_________/ \__/ | | | | | scheme authority path query fragment Joomla Community resources https://joomla.org/ Joomla Community chat in Mattermost WebTolk Joomla extensions This article in Russian
When building a url in the code, you can collect all the strings with concatenation of the form
$url = $domain.'/index.php?option='.$option.'&view='.$view.'¶m1='.$value1;
and for small strings, it can even be convenient. But if there are a lot of parameters or they require standardization/cleaning in the process, then not everything is so convenient and obvious. For example, there may be a leading slash in part of the url (a slash at the beginning of the url fragment) and the incoming domain for the request may also end in a slash, and so we get an incorrect url for the request with a double slash somewhere in the middle...
For the tasks of standardization and uniformity of url retrieval, Joomla has the Joomla\Uri\Uri
class. Previously, with Joomla 1.6 and older, it was called JUri
. This class provides work with urls according to the RFC3986 standard and takes over the work of parsing URLs for spare parts or assembling URLs from spare parts))
Example: get a specific parameter from the url.
use Joomla\Uri\Uri;
$url = 'https://web-tolk.ru/dev/biblioteki?param=value';
$uri = new Uri($url);
// output the line 'value' here
echo $uri->getVar('param');
Yes, there is a native PHP function parse_url
, you might say... But the Uri
class ensures safe operation with UTF-8 in urls, including Cyrillic domains. In order not to write various checks yourself, you can use the features of the Joomla core.
How to build required url in the code in Joomla
Here, too, everything is simple:
use Joomla\Uri\Uri;
$uri = new Uri;
$uri->setHost('web-tolk.ru');
$uri->setScheme('https');
// setPath() starts with leading slash
$uri->setPath('/dev/biblioteki');
// GET-parameters can be passed as an array
$vars = [
'param1' => 'value1',
'param2' => 'value2',
'param3' => 'value3',
];
$uri->setQuery($vars);
// echo our url like string
echo $uri->toString();
The hierarchy of Uri
classes in Joomla is designed so that getter methods are in the AbstractUri
class, and setters are in the Uri
class. The setters can be viewed in the file libraries/vendor/joomla/uri/src/Uri.php . We look at Hetaera in the file libraries/vendor/joomla/uri/src/AbstractUri.php.
If you have PHPStorm, then it knows Joomla perfectly well and tells you everything you need.
Well, you can give a link to the old documentation page, which is still mostly relevant, adjusted for the use of namespaces.
Uri structure:
foo://example.com:8042/over/there?name=ferret#nose
\_/ \______________/\_________/ \_________/ \__/
| | | | |
scheme authority path query fragment