madewithlove/htaccess-api-client

全球最佳htaccess测试器的API客户端。

资助包维护!
madewithlove

安装次数: 17,436

依赖项: 1

建议者: 0

安全性: 0

星级: 9

关注者: 11

分支: 1

开放问题: 2

类型:package

v2.3.0 2021-11-26 12:52 UTC

README

Build status Latest Stable Version License codecov

这是一个用于与Htaccess测试器交互的API客户端。

安装

composer require madewithlove/htaccess-api-client

使用方法

该包可以与任何PSR兼容的HTTP客户端一起使用。在这个例子中,我们将使用guzzle的PSR适配器。

use Http\Factory\Guzzle\ServerRequestFactory;
use Http\Adapter\Guzzle6\Client;
use Madewithlove\HtaccessClient

$client = new HtaccessClient(
    new Client(),
    new ServerRequestFactory()
);

$response = $client->test(
    'http://localhost',
    'RewriteRule .* /foo [R]'
);

$response->getOutputUrl(); // "http://localhost/foo"
$response->getLines();
/*
array(1) {
  [0]=>
  object(Madewithlove\ResultLine)#30 (5) {
    ["line":"Madewithlove\ResultLine":private]=> string(23) "RewriteRule .* /foo [R]"
    ["message":"Madewithlove\ResultLine":private]=> string(98) "The new url is http://localhost/foo
Test are stopped, a redirect will be made with status code 302"
    ["isMet":"Madewithlove\ResultLine":private]=> bool(true)
    ["isValid":"Madewithlove\ResultLine":private]=> bool(true)
    ["wasReached":"Madewithlove\ResultLine":private]=> bool(true)
  }
}
*/

服务器变量

Htaccess测试器支持传递服务器变量以供重写规则评估。我们目前支持以下变量。

服务器变量可以通过test()share()方法传递。

$serverVariables = ServerVariables::default()->with(
    'SERVER_NAME',
    'example.com'
);

$response = $client->test(
    'http://localhost',
    'RewriteCond %{SERVER_NAME} example.com
    RewriteRule .* /foo [R]',
    $serverVariables
);

$response = $client->share(
    'http://localhost',
    'RewriteCond %{SERVER_NAME} example.com
    RewriteRule .* /foo [R]',
    $serverVariables
);