wa72/url

用于处理和操作URL的类

v0.7.1 2018-07-25 15:54 UTC

This package is auto-updated.

Last update: 2024-09-18 04:25:29 UTC


README

PHP类,用于处理和操作URL。它是一个完全独立的框架单类库。

Build Status Latest Version

  • 解析URL字符串为对象

  • 添加和修改查询参数

  • 设置和修改URL的任何部分

  • 以PHP风格测试URL的查询参数是否相等

  • 支持相对URL

  • 将绝对、主机相对和协议相对的URL转换为相对URL,反之亦然

  • 版本0.7(2018/07/25)的新功能:可选兼容性 Psr\Http\Message\UriInterface (PSR-7),见下文

安装

此包已在 Packagist 上列出。

composer require wa72/url

功能和用法

将URL解析为对象

use \Wa72\Url\Url;

$url = new Url('http://my-server.com/index.php?p1=foo&p2=bar');
// or alternatively use the static factory function `parse`:
$url = Url::parse('http://my-server.com/index.php?p1=foo&p2=bar');

// set another host
$url->setHost('another-server.org');

// return the URL as string again
echo $url->write();
// or simply:
echo $url;

轻松修改和添加查询参数

$url->setQueryParameter('p1', 'newvalue');
$url->setQueryParameter('param3', 'another value');
echo $url;
// will output:
// http://another-server.org/index.php?p1=newvalue&p2=bar&param3=another%20value

// You can even add arrays a query parameter:
$url->setQueryParameter('param3', array(5, 6));
echo $url;
// will output:
// http://another-server.org/index.php?p1=newvalue&p2=bar&param3[]=5&param3[]=6

以PHP方式比较带有查询字符串的URL

虽然通常URL可能包含多个同名查询参数(例如 ?a=value1& a=value2& a=value3),并且有一些Web应用程序将这些参数转换为数组,但这不是PHP处理查询参数的方式:在PHP中,最后具有相同名称的参数总是优先,因此上面的查询字符串等于只有 ?a=value3

同样,虽然通常查询字符串中查询参数的顺序可能对Web应用程序很重要,但在PHP中并不重要:对于PHP应用程序,?a=1& b=2 等于 ?b=2& a=1

Url处理URL中的查询字符串的方式与PHP相同,因此以下示例中的URL被认为是相等的

$url1 = Url::parse('index.php?a=0&a=1&b=2');
$url2 = Url::parse('index.php?b=2&a=1');

return $url1.equals($url2);
// will return TRUE

将相对URL转换为绝对URL

给定一个具有以下属性的URL

  • 没有方案(协议相对URL)
  • 没有方案和主机(主机相对URL)
  • 没有方案、没有主机和相对路径(相对URL)

可以由给定的基本URL转换为绝对URL

$url = Url::parse('page.php');
$baseurl = Url::parse('http://www.test.test/index.html');
$url->makeAbsolute($baseurl);
echo $url; // will print: http://www.test.test/page.php

$url = Url::parse('../de/seite.html');
$baseurl = Url::parse('http://www.test.test/en/page.html');
$url->makeAbsolute($baseurl);
echo $url; // will print: http://www.test.test/de/seite.html

$url = Url::parse('/index.html');
$baseurl = Url::parse('http://www.test.test/en/page.html');
$url->makeAbsolute($baseurl);
echo $url; // will print: http://www.test.test/index.html

$url = Url::parse('/index.html');
$baseurl = Url::parse('http://www.test.test/en/page.html');
$url->makeAbsolute($baseurl);
echo $url; // will print: http://www.test.test/index.html

$url = Url::parse('//www.test.test/index.html');
$baseurl = Url::parse('https://www.test.test/en/page.html');
$url->makeAbsolute($baseurl);
echo $url; // will print: https://www.test.test/index.html

输出协议相对和主机相对的URL

如果您在输出URL时想省略方案,或者方案和主机,可以将 Url::WRITE_FLAG_OMIT_SCHEMEUrl::WRITE_FLAG_OMIT_HOST 传递给 write() 方法

$url = Url::parse('https://www.test.test/index.php?id=5#c1');

// protocol-relative output
echo $url->write(Url::WRITE_FLAG_OMIT_SCHEME); // will print: //www.test.test/index.php?id=5#c1

// host-relative output
echo $url->write(Url::WRITE_FLAG_OMIT_SCHEME | Url::WRITE_FLAG_OMIT_HOST)); // will print: /index.php?id=5#c1

Psr\Http\Message\UriInterface (PSR-7) 的兼容性

  • Url 现在定义了此接口中定义的所有方法,但并未正式实现它。
  • 创建了一个包装类 PsrUri,该类实现了 UriInterface
  • 为在 UrlPsr7Uri 之间进行转换提供了方法

Url 不直接实现PSR接口,原因如下

  1. 不引入对PSR接口的新依赖。依赖关系仅在composer json中建议。
  2. 因为PSR接口设计为不可变,而 Url 不是。

要使用此功能,您需要 composer require psr/http-message

<?php
use Wa72\Url\Psr7Uri;
use Wa72\Url\Url;

# Get a Psr7Uri from a Url object

$url = Url::parse('https://www.foo.bar/test.php?a=b');
$psr7uri = Psr7Uri::fromUrl($url);
// or alternatively:
$psr7uri = $url->toPsr7();

# Get a Url object from UriInterface

$url = Url::fromPsr7($psr7uri); // this works for every UriInterface object, not only Wa72\Url\Psr7Uri
// or alternatively:
$url = $psr7uri->toUrl();

# You can also create a Psr7Uri directly

$psr7uri = Psr7Uri::parse('https://www.foo.bar/test.php?a=b');

更多文档即将到来

同时,请查看源代码,其中包含大量注释。

(c) Christoph Singer 2018. MIT许可证。