joomla / uri
Joomla Uri 包
Requires
- php: ^8.1.0
Requires (Dev)
- phan/phan: ^5.4.2
- phpstan/phpstan: ^1.10.7
- phpunit/phpunit: ^9.5.28
- squizlabs/php_codesniffer: ^3.7.2
Suggests
- ext-mbstring: Used to speed up url parsing
This package is auto-updated.
Last update: 2024-09-03 20:06:35 UTC
README
简介
Joomla 框架包括一个 Uri 包,该包允许使用多种有用的方法来操作 Uri 字符串的一部分,同时处理 uri。
Uri 包包含的类有 Uri,它扩展了 UriAbstract 类,实现了 UriInterface。另一个类是 UriHelper。
Uri 类是一个可变对象,用于操作 Uri。
要传递一个 uri 作为值,请使用 UriImmutable,该对象保证你传递的对象不能被代码操纵,这可能导致你的代码出现错误。
如果只需要读取访问,建议对 UriInterface 进行类型提示。这样就可以传递一个 Uri 或 UriImmutable 对象。
UriHelper 类只包含一个方法 parse_url(),这是 PHP 的 parse_url() 的 UTF-8 安全替代方案。
处理 Uri 时,您可以使用多种方式使用 Uri 类。使用 Uri 类提供的方法以编程方式构建 Uri 非常简单。
用法
Uri 类提供的方法允许您操作 uri 的所有方面。例如,假设您想要设置一个新的 uri,添加端口号,然后还发送用户名和密码来验证 .htaccess 安全文件,您可以使用以下语法
<?php // new uri object $uri = new Joomla\Uri\Uri; $uri->setHost('https://'); $uri->setPort('8888'); $uri->setUser('myUser'); $uri->setPass('myPass'); echo $uri->__toString();
这将输出
myUser:myPass@https://:8888
如果您想要在主机后添加特定的文件路径,则可以使用 setPath() 方法
<?php // set path $uri->setPath('path/to/file.php');
这将输出 myUser:myPass@https://:8888path/to/file.php
输出
<?php // url query $uri->setQuery('foo=bar');
Output
myUser:myPass@https://:8888path/to/file.php?foo=bar
通过 Composer 安装
将 "joomla/uri": "~3.0" 添加到 composer.json 中的 require 块,然后运行 composer install。
{
"require": {
"joomla/uri": "~3.0"
}
}
或者,您可以直接在命令行中运行以下命令
composer require joomla/uri "~3.0"