aura / uri
Aura Uri 包提供构建和操作 URL 字符串的对象。
2.0.0
2023-09-13 07:29 UTC
Requires
- php: ^5.4 || ^7.0 || ^8.0
Requires (Dev)
- phpunit/phpunit: ^5.7 || ^4.8 || ^9.5
- yoast/phpunit-polyfills: ^1.1
README
Auri.Uri
包提供对象来帮助您创建和操作 URL,包括查询字符串和路径元素。它是通过分解 URL 的各个部分并允许您单独修改它们来实现的;然后您可以将其作为一个单独的 URL 字符串检索。这在构建复杂链接时非常有用,例如在分页导航系统中。
此包符合 PSR-0、PSR-1 和 PSR-2。如果您发现符合性疏忽,请通过拉取请求发送补丁。
入门
实例化
开始使用最简单的方法是使用 UrlFactory 创建一个 Url 对象。
<?php use Aura\Uri\Url\Factory as UrlFactory; use Aura\Uri\PublicSuffixList; $psl = new PublicSuffixList(require '/path/to/Aura.Uri/data/public-suffix-list.php'); $url_factory = new UrlFactory($_SERVER, $psl); $url = $url_factory->newCurrent();
您可以从 URL 字符串中填充 URL 属性
<?php $string = 'http://anonymous:guest@example.com/path/to/index.php/foo/bar.xml?baz=dib#anchor'); $url = $url_factory->newInstance($string); // now the $url properties are ... // // $url->scheme => 'http' // $url->user => 'anonymous' // $url->pass => 'guest' // $url->host => Aura\Uri\Host, with these methods: // ->get() => 'example.com' // ->getSubdomain() => null // ->getRegisterableDomain() => 'example.com' // ->getPublicSuffix() => 'com' // $url->port => null // $url->path => Aura\Uri\Path, with these ArrayObject elements: // ['path', 'to', 'index.php', 'foo', 'bar'] // and this method: // ->getFormat() => '.xml' // $url->query => Aura\Uri\Query, with these ArrayObject elements: // ['baz' => 'dib'] // $url->fragment => 'anchor'
或者,您可以使用工厂创建一个表示当前网络请求 URI 的 URL
<?php $url = $url_factory->newCurrent();
操作
在创建 URL 对象后,我们可以修改其组成部分,然后从修改后的对象中检索新的 URL 字符串。
<?php // start with a full URL $string = 'http://anonymous:guest@example.com/path/to/index.php/foo/bar.xml?baz=dib#anchor'; $url = $url_factory->newInstance($string); // change to 'https://' $url->setScheme('https'); // remove the username and password $url->setUser(null); $url->setPass(null); // change the value of 'baz' from 'dib' to 'zab' $url->query->baz = 'zab'; // add a new query element called 'zim' with a value of 'gir' $url->query->zim = 'gir'; // reset the path to something else entirely. // this will additionally set the format to '.php'. $url->path->setFromString('/something/else/entirely.php'); // add another path element $url->path[] = 'another'; // get the url as a string; this will be without the scheme, host, port, // user, or pass. $new_url = $url->get(); // the $new_url string is as follows; notice how the format // is always applied to the last path-element: // /something/else/entirely/another.php?baz=zab&zim=gir#anchor // get the full url string, including scheme, host, port, user, and pass. $full_url = $url->getFull(); // the $full_url string is as follows: // https://example.com/something/else/entirely/another.php?baz=zab&zim=gir#anchor
公共后缀列表主机解析
主机组成部分
除了创建和操作 URL,Aura.Uri
还能够将主机解析为其组成部分,即主机的子域名、注册域和公共后缀。主机的组成部分可以通过 Aura.Uri 主机对象的属性获得,如上述示例所示。
公共后缀列表
这种解析能力是通过 Mozilla 的社区资源和倡议 Public Suffix List 实现的。
更新公共后缀列表
由于公共后缀列表是一个外部资源且是一个活文档,因此定期更新您的列表副本很重要。您可以通过执行提供的 update.php
脚本来完成此操作。
php /path/to/Aura.Uri/scripts/update.php
执行 update.php
将检索最新的公共后缀列表版本,将其解析为数组,并将其存储在 /path/to/Aura.Uri/data
目录中。