Joomla 提示:使用 JoomlaUriUri 类创建 URL(创建.提示.Joomla.URL.JoomlaUriUri...)
在Joomla中构建URL,您可以使用字符串拼接:
$url = $domain.'/index.php?option='.$option.'&view='.$view.'¶m1='.$value1;
这种方法对于简单的URL适用,但参数过多或需要URL标准化时,就会变得繁琐且易出错。例如,路径片段可能包含多余的斜杠,导致URL错误。
为了解决这个问题,Joomla提供了JoomlaUriUri类(在Joomla 1.6及更早版本中称为JURI)。该类遵循RFC 3986标准,简化了URL的解析和构建。它确保UTF-8安全操作,尤其适用于包含西里尔字母的域名。相比之下,原生PHP函数parse_url在处理UTF-8方面存在不足。
示例:从URL中获取参数
use JoomlaUriUri; $url = 'https://web-tolk.ru/dev/biblioteki?param=value'; $uri = new Uri($url); echo $uri->getVar('param'); // 输出 'value'
在Joomla代码中构建URL
use JoomlaUriUri; $uri = new Uri; $uri->setHost('web-tolk.ru'); $uri->setScheme('https'); $uri->setPath('/dev/biblioteki'); // setPath() 自动添加前导斜杠 $vars = [ 'param1' => 'value1', 'param2' => 'value2', 'param3' => 'value3', ]; $uri->setQuery($vars); echo $uri->toString();
JoomlaUriUri类的设计遵循清晰的层次结构:AbstractUri类包含getter方法,Uri类包含setter方法。 您可以查看libraries/vendor/joomla/uri/src/uri.php和libraries/vendor/joomla/uri/src/abstracturi.php文件了解详细信息。PhpStorm对Joomla有良好的支持,可以方便地查看类的方法和属性。
旧版本的文档仍然部分适用,但需要根据新的命名空间进行调整。
URL结构:
foo://example.com:8042/over/there?name=ferret#nose _/ ______________/_________/ _________/ __/ | | | | | scheme authority path query fragment
Joomla社区资源:
- https://www.php.cn/link/4d2356339f13e4b3d5aec55ba88a6019
- Joomla社区Mattermost聊天
- WebTolk Joomla扩展
- (俄文)相关文章
以上就是Joomla 提示:使用 JoomlaUriUri 类创建 URL的详细内容,更多请关注知识资源分享宝库其它相关文章!