pagemill / router
PHP请求路由器
Requires
- php: ^7.4 || ^8.0
- pagemill/accept: ~2.0
- pagemill/pattern: ^2.0.1
Requires (Dev)
- phpunit/phpunit: ^9.0
This package is auto-updated.
Last update: 2024-09-11 01:06:02 UTC
README
此库为网络请求确定路由。它易于使用且速度快。
基本路由
对于最基本的路由需求,可以将一个数组直接传递给Router类构造函数。每个路由数组应包含一个type
、pattern
和一个action
或子路由数组routes
。虽然action
对路由器本身没有意义,但其目的是用于匹配后。
$r = new PageMill\Router\Router( [ [ "type" => "exact", "pattern" => "/foo", "action" => "Foo" ], [ "type" => "exact", "pattern" => "/foo/bar", "action" => "FooBar" ], ] ); $route = $r->match("/foo");
使用方法添加
您也可以使用add
方法添加路由。
$r = new PageMill\Router\Router(); $r->add("exact", "/foo", "Foo"); $r->add("exact", "/foo/bar/", "FooBar"); $route = $r->match("/foo");
如果性能很重要,建议将路由传递给构造函数。
匹配类型
支持三种匹配类型,以及一个默认路由。
exact - 请求路径必须完全匹配pattern
。
starts_with - 当请求路径以pattern
的确切值开头时匹配。
regex - 使用正则表达式匹配请求路径。
default - 如果没有其他路由匹配,将使用此默认路由。默认路由不需要pattern
。只能定义一个默认路由。
匹配令牌
可选地,一个路由可以包含一个令牌数组,允许使用请求路径中的值填充match
方法返回值中的命名数组。这仅适用于starts_with
和exact
匹配类型。
对于starts_with
匹配,令牌将基于请求路径中出现的值填充(这些值位于斜杠(/
)之间,但不包括模式本身)。
$r = new PageMill\Router\Router( [ [ "type" => "starts_with", "pattern" => "/foo/", "action" => "Foo", "tokens" = [ "group", "id" ] ] ] ); $route = $r->match("/foo/1/2/");
$route
的值将是
array( "type" => "starts_with", "pattern" => "/foo/", "action" => "Foo", "tokens" = [ "group" => 1, "id" => 2 ] )
对于regex
匹配,令牌将基于正则表达式中的反向引用填充。
$r = new PageMill\Router\Router( [ [ "type" => "regex", "pattern" => "/foo/(\d+)/(\d+)/", "action" => "Foo", "tokens" = [ "group", "id" ] ] ] ); $route = $r->match("/foo/1/2/");
$route
的值将是
[ "type" => "regex", "pattern" => "/foo/", "action" => "Foo", "tokens" = [ "group" => 1, "id" => 2 ] ]
匹配主机名、请求方法、Accept头和其他HTTP头
还可以通过将设置添加到路由数组中,匹配主机名、请求方法、Accept头和其他HTTP头。
host
- 匹配HTTP Host头。method
- 匹配HTTP请求方法(GET、POST等)accept
- 验证Accept头是否包含列表中的一种MIME类型。headers
- 一个包含头和匹配模式的数组。
要匹配主机名,将host
添加到路由配置中。要仅匹配GET
请求,添加method
。
$r = new PageMill\Router\Router( [ [ "type" => "regex", "pattern" => "/foo/(\d+)/(\d+)/", "action" => "Foo", "host" => "www.example.com", "method" => "GET" ] ] ); $route = $r->match("/foo/1/2/");
$route
的值将是
[ "type" => "starts_with", "pattern" => "/foo/", "action" => "Foo", "tokens" = [ "group" => 1, "id" => 2 ] "host" => "www.example.com", "method" => "GET" ]
默认情况下,这被视为精确匹配。您可以通过提供一个设置type
和pattern
的数组来进行更复杂的匹配。这适用于host
、method
和headers
数组中的模式。
$r = new PageMill\Router\Router( [ [ "type" => "regex", "pattern" => "/foo/(\d+)/(\d+)/", "action" => "Foo", "host" => [ "type" => "regex", "pattern" => '/\.example\.com$/' ], "method" => "GET" ] ] ); $route = $r->match("/foo/1/2/");
这些设置的匹配值将返回在匹配的路由数组中。$route
的值将是
[ "type" => "starts_with", "pattern" => "/foo/", "action" => "Foo", "tokens" = [ "group" => 1, "id" => 2 ] "host" => "www.example.com", "method" => "GET" ]
要验证Accept头与MIME类型的列表匹配,可以使用此路由配置。例如,假设客户端的Accept头包含text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
。
注意:与其他选项不同,accept
选项只接受单个字符串MIME类型或MIME类型数组。
$r = new PageMill\Router\Router( [ [ "type" => "regex", "pattern" => "/foo/(\d+)/(\d+)/", "action" => "Foo", "accept" => [ "text/html" ] ] ] ); $route = $r->match("/foo/1/2/");
$route
的值将是
[ "type" => "starts_with", "pattern" => "/foo/", "action" => "Foo", "tokens" = [ "group" => 1, "id" => 2 ] "accept" => "text/html" ]
路由器将尊重来自Accept头部的客户端质量评分。有关在HTTP客户端Accept头部中使用的质量评分的解释,请参阅 https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html。如果匹配的MIME类型的质量评分相等,将按照配置中定义的顺序进行尊重。
$r = new PageMill\Router\Router( [ [ "type" => "regex", "pattern" => "/foo/(\d+)/(\d+)/", "action" => "Foo", "accept" => [ "application/json" "text/html" ] ] ] ); $route = $r->match("/foo/1/2/");
因为"text/html"的质量评分在Accept头部中未定义,它被假定为1。$route的值将是
[ "type" => "starts_with", "pattern" => "/foo/", "action" => "Foo", "tokens" = [ "group" => 1, "id" => 2 ] "accept" => "text/html" ]
要匹配任意的HTTP头部,请在路由配置中添加headers
。
此示例将确保Authorization
头部包含12345678
。
$r = new PageMill\Router\Router( [ [ "type" => "regex", "pattern" => "/foo/(\d+)/(\d+)/", "action" => "Foo", "headers" => [ "Authorization" => [ "type" => "exact", "pattern" => '12345678' ] ] ] ] ); $route = $r->match("/foo/1/2/");
$route
的值将是
[ "type" => "starts_with", "pattern" => "/foo/", "action" => "Foo", "tokens" = [ "group" => 1, "id" => 2 ] "headers" => [ "Authorization" => "12345678" ] ]
默认路由
$r = new PageMill\Router\Router( [ [ "type" => "exact", "pattern" => "/foo/", "action" => "Foo", ], [ "type" => "default", "action" => "Default", ] ] ); $route = $r->match("/foo/1/2/");
$route
的值将是
[ "type" => "default", "action" => "Default", ]
注意:对于默认路由,不会返回令牌,并且不会对路径执行模式匹配。
只允许一个默认路由。如果定义了多个默认路由,将抛出InvalidRoute
异常。
添加子路由(也称为路由映射)
如果您有多个路由,它们都具有相同的前缀或匹配相同的模式,将那些路由作为更一般路由的子路由分组可能是有益的。例如,如果我们有多个位于/foo
路径下的路由,我们可以这样配置我们的路由:
$r = new PageMill\Router\Router( [ [ "type" => "starts_with", "pattern" => "/foo", "routes" => [ [ "type" => "exact", "pattern" => "/foo/bar", "action" => "FooBar" ], [ "type" => "exact", "pattern" => "/foo/baz", "action" => "FooBaz" ], ] ] ] ); $route = $r->match("/foo/bar");
$route
的值将是
[ "type" => "exact", "pattern" => "/foo/bar", "action" => "FooBar" ]
保存路由列表
如果您想保存路由列表以供重用,可以调用getRoutes()
方法。
$r = new PageMill\Router\Router( [ [ "type" => "exact", "pattern" => "/foo/", "action" => "Foo", ], [ "type" => "default", "action" => "Default", ] ] ); $routes = $r->get_routes();