simplon / url
简单URL
0.6.1
2020-04-17 20:06 UTC
Requires
- php: >=7.2
Requires (Dev)
- phpstan/extension-installer: dev-master
- phpstan/phpstan: ^0.12.0@dev
- phpunit/phpunit: ^6.0
- slevomat/coding-standard: dev-master
- squizlabs/php_codesniffer: ^3.0@dev
README
_ _ _
___(_)_ __ ___ _ __ | | ___ _ __ _ _ _ __| |
/ __| | '_ ` _ \| '_ \| |/ _ \| '_ \ | | | | '__| |
\__ \ | | | | | | |_) | | (_) | | | | | |_| | | | |
|___/_|_| |_| |_| .__/|_|\___/|_| |_| \__,_|_| |_|
|_|
Simplon Url
简单的URL解析器和构建器。
需求
- PHP7.2+
解析
HTTP及其类似
解析URL就像以下示例一样简单
$url = new Url( 'http://foo.bar.com/en/test/challenge?utm_source=source&utm_campaign=campaign&utm_medium=medium#hello-world' ); $url->getProtocol(); // http $url->getHost(); // foo.bar.com $url->getSubDomain(); // foo $url->getDomain(); // bar $url->getTopLevelDomain(); // com $url->getPath(); // /en/test/challenge $url->getAllQueryParams(); // ['utm_source' => 'source', ...] $url->getQueryParam('utm_source'); // source $url->getPathSegment(1); // en $url->getPathSegment(2); // test $url->getFragment(); // hello-world
FTP及其类似
$url = new Url( 'ftp://peter:sunny@foobar.com:21' ); $url->getUser(); // peter $url->getPass(); // sunny $url->getPort(); // 21
构建/操作
您可以从头构建URL或从现有URL进行操作。
构建新URL
$url = (new Url()) ->withProtocol('https') ->withHost('dear-johnny.io') ->withPath('/foo/bar') ->withQueryParam('sun', 'is-shining') ->withQueryParam('training', 'yes') ->withFragment('hello-world'); echo $url; // https://dear-johnny.io/foo/bar?sun=is-shining&training=yes#hello-world
操作现有URL
$url = new Url( 'https://us.dear-johnny.io/foo/bar?sun=is-shining&training=yes#hello-world' ); $url ->withoutSubDomain() ->withTopLevelDomain('com') ->withPathSegment(1, 'hoo') ->withPrefixPath('/en') ->withTrailPath('/much/more') ->withoutQueryParam('training') ->withQueryParam('sun', 'off') ->withoutFragment(); echo $url; // https://dear-johnny.com/en/hoo/bar/much/more?sun=off
与路径占位符一起工作
路径占位符适用于以下路径操作。占位符是可选的,并按以下方式工作
// // withPath // $route = '/say/{message}'; $url = new Url('https://foobar.io'); $url->withPath($route, ['message' => 'hello']); echo $url; // https://foobar.io/say/hello // // withPrefixPath // $route = '/hello/{message}'; $url = new Url('https://foobar.io/bob'); $url->withPrefixPath($route, ['message' => 'there']); echo $url; // https://foobar.io/hello/there/bob // // withTrailPath // $route = '/got/{count}/{item}'; $url = new Url('https://foobar.io/bob'); $url->withTrailPath($route, ['count' => 'five', 'item' => 'cars']); echo $url; // https://foobar.io/bob/got/five/cars
许可证
Simplon Url可以在MIT许可证的条款下免费分发。
版权 (c) 2017 Tino Ehrich (tino@bigpun.me)
特此授予任何获得本软件及其相关文档副本(“软件”)的人,免费使用软件的权利,不受限制,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或销售软件副本的权利,并允许获得软件的人这样做,前提是满足以下条件
上述版权声明和本许可声明应包含在软件的所有副本或主要部分中。
软件按“原样”提供,不提供任何形式的保证,无论是明示的、暗示的还是其他的,包括但不限于适销性、针对特定目的的适用性和非侵权性。在任何情况下,作者或版权所有者均不对任何索赔、损害或其他责任承担责任,无论这种责任是基于合同、侵权或其他原因,是否因软件或软件的使用或其他方式产生。