spatie / url
解析、构建和操作 URL
2.4.0
2024-03-08 11:35 UTC
Requires
- php: ^8.0
- psr/http-message: ^1.0 || ^2.0
- spatie/macroable: ^1.0 || ^2.0
Requires (Dev)
- pestphp/pest: ^1.21
README
一个简单的包,用于处理应用程序中的 URL。
安装
您可以通过 composer 安装此包
composer require spatie/url
使用方法
解析和转换 URL
检索 URL 的任何部分
use Spatie\Url\Url; $url = Url::fromString('https://spatie.be/opensource'); echo $url->getScheme(); // 'https' echo $url->getHost(); // 'spatie.be' echo $url->getPath(); // '/opensource'
转换 URL 的任何部分
注意
Url
类是不可变的。
$url = Url::fromString('https://spatie.be/opensource'); echo $url->withHost('github.com')->withPath('spatie'); // 'https://github.com/spatie'
协议
转换 URL 协议。
$url = Url::fromString('http://spatie.be/opensource'); echo $url->withScheme('https'); // 'https://spatie.be/opensource'
使用允许的协议列表。
注意 列表中的每个协议都将被清理
$url = Url::fromString('https://spatie.be/opensource'); echo $url->withAllowedSchemes(['wss'])->withScheme('wss'); // 'wss://spatie.be/opensource'
或直接传递给 fromString
作为 URL 的协议将被清理和验证
$url = Url::fromString('https://spatie.be/opensource', [...SchemeValidator::VALID_SCHEMES, 'wss']); echo $url->withScheme('wss'); // 'wss://spatie.be/opensource'
查询参数
检索和转换查询参数
$url = Url::fromString('https://spatie.be/opensource?utm_source=github&utm_campaign=packages'); echo $url->getQuery(); // 'utm_source=github&utm_campaign=packages' echo $url->getQueryParameter('utm_source'); // 'github' echo $url->getQueryParameter('utm_medium'); // null echo $url->getQueryParameter('utm_medium', 'social'); // 'social' echo $url->getQueryParameter('utm_medium', function() { //some logic return 'email'; }); // 'email' echo $url->withoutQueryParameter('utm_campaign'); // 'https://spatie.be/opensource?utm_source=github' echo $url->withQueryParameters(['utm_campaign' => 'packages']); // 'https://spatie.be/opensource?utm_source=github&utm_campaign=packages'
路径段
检索路径段
$url = Url::fromString('https://spatie.be/opensource/laravel'); echo $url->getSegment(1); // 'opensource' echo $url->getSegment(2); // 'laravel'
PSR-7 UriInterface
实现了 PSR-7 的 UriInterface
接口
class Url implements UriInterface { /* ... */ }
相比这个包,league/uri
是一个功能更强大的包。此包存在的主要原因是因为其他替代方案需要非标准的 php 扩展。如果您处理特殊字符编码或需要防弹验证,您肯定更愿意使用 league/uri
。
Spatie 是一家位于比利时的安特卫普的网页设计公司。您可以在我们的网站上找到所有开源项目的概述 在这里。
测试
composer test
支持我们
我们在创建 一流的类开源包 上投入了大量资源。您可以通过 购买我们的付费产品之一 来支持我们。
我们非常感谢您从家乡寄给我们明信片,注明您正在使用我们的哪个包。您可以在我们的 联系页面 上找到我们的地址。我们将在 我们的虚拟明信片墙 上发布所有收到的明信片。
更新日志
请参阅 CHANGELOG 了解最近更改的更多信息。
贡献
请参阅 CONTRIBUTING 了解详细信息。
安全漏洞
请参阅 我们的安全策略 了解如何报告安全漏洞。
明信片软件
您可以自由使用此包,但如果它进入您的生产环境,我们非常感谢您从家乡寄给我们明信片,注明您正在使用我们的哪个包。
我们的地址是:Spatie,Kruikstraat 22,2018 安特卫普,比利时。
我们将在我们的公司网站上发布所有收到的明信片 在这里。
鸣谢
许可协议
MIT 许可证 (MIT)。请参阅 许可文件 了解更多信息。