binsoul/net-http-router

PSR-7请求的Web路由实现

dev-master / 1.0.x-dev 2016-04-27 17:50 UTC

This package is auto-updated.

Last update: 2024-09-14 22:10:13 UTC


README

Latest Version on Packagist Software License Total Downloads

此包为PSR-7请求提供Web路由实现。它使用匹配器从给定请求中提取信息,并返回一个路由对象。

您的应用程序可以使用匹配路由提供的信息来调度它。此包不提供调度器。

安装

通过composer

$ composer require binsoul/net-http-router

匹配器

匹配器可以是一个闭包、一个具有 __invoke 方法的对象,或者实现 Matcher 接口的对象。

如果一个匹配器决定找到了路由,它可以选择设置一个响应

$router->addMatcher(
    function (Route $route)
    {
        if ($route->getMissingPath() == '/hello') {
            $route->found(new Response('Hello world!'));
        }
    }
);

$route = $router->match($request); // http:/domain/hello
if ($route->hasResponse()) {
    echo $route->getResponse(); // Hello world!
}

StaticMatcher

StaticMatcher用于匹配简单路径并设置匹配的提供路由参数。

以下示例

$router->addMatcher(
    new StaticMatcher(
        [
            '/blog' => ['responder' => 'Blog']
        ]
    )
);

$route = $router->match($request); // http://domain/blog
var_export($route->getData());  

将输出

array('responder' => 'Blog',)

RegexMatcher

RegexMatcher用于匹配任意正则表达式。命名捕获组设置为路由的参数。

以下示例

$router->addMatcher(
    new RegexMatcher(
        [
            '/edit/(?<id>[0-9]+)' => ['responder' => 'Edit']
        ]
    )
);

$route = $router->match($request); // http://domain/edit/1
var_export($route->getData());  

将输出

array('id' => 1, 'responder' => 'Edit',)

ParameterMatcher

ParameterMatcher用于匹配具有参数占位符的路径。

占位符可以定义在以下格式中

[prefix+][name][=format[(length)]][?]
  • 前缀:除 "]" 和 "+" 之外任意数量的字符,后跟一个 "+"。
  • 名称:单个字符后跟字符、数字或 "_"。
  • 格式: "=" 后跟一个定义的格式名称,可选地后跟长度定义。
  • 长度:单个数字或括号内的数字范围。
  • 标记:如果定义以单个 "?" 结束,则参数是可选的。

例如,路径

/[year=number(4)][/+month=number(1-2)?]/[name].html

将匹配

/2015/09/article.html
/2015/9/article.html
/2015/article.html

找到的占位符被设置为路由的参数。以下示例

$router->addMatcher(
    new ParameterMatcher(
        [
            '/[year]/[month]/[name].html' => ['responder' => 'Blog']
        ]
    )
);

$route = $router->match($request); // http://domain/2015/09/article.html
var_export($route->getData());  

将输出

array('year' => '2015', 'month' => '09', 'name' => 'article', 'responder' => 'Blog',)

NamespaceMatcher

NamespaceMatcher允许将其他匹配器分组在共同的路径前缀下

以下定义

$matcher = new NamespaceMatcher(
    '/admin',
    [
        new StaticMatcher(
            [
                '/' => ['responder' => 'Home'],
                '/list' => ['responder' => 'List'],
            ]
        ),
        new RegexMatcher(
            [
                '/edit/(?<id>[0-9]+)' => ['responder' => 'Edit'],
            ]
        ),
    ]
);

将匹配

/admin/
/admin/list
/admin/edit/1

路由器

匹配器可以注册为闭包、具体对象或字符串。如果匹配器作为字符串注册,则提供的工厂用于延迟构建匹配器对象。

$router->addMatcher('AccountMatcher');
$router->setFactory($factory);

// will call $factory->buildMatcher('AccountMatcher');
$router->match($request); 

匹配器被添加到内部队列中,默认路由器将按它们添加到队列的顺序处理它们。

例如,如果队列构建如下

$router->addMatcher('Foo'); // matches /foo
$router->addMatcher('Bar'); // matches /bar
$router->addMatcher('Baz'); // matches /baz

$router->match($request); // http://domain/foo/bar/baz

路由对象将按如下方式更改

Foo is called with missing path = '/foo/bar/baz'    and matched path = ''
Bar is called with missing path = '/bar/baz'        and matched path = '/foo'
Baz is called with missing path = '/baz'            and matched path = '/foo/bar'

测试

$ composer test

许可证

MIT许可证(MIT)。有关更多信息,请参阅许可证文件