anax / router
Anax 路由模块,将请求路由到处理器。
v2.1.0
2020-05-19 09:32 UTC
Requires
- php: >=7.2
- anax/commons: ^2.0.0@alpha
- psr/container: ^1.0
Requires (Dev)
- anax/configure: ^2.0.0@alpha
- anax/di: ^2.0.0@alpha
- phpunit/phpunit: ^8
- dev-master
- v2.1.0
- v2.0.1
- v2.0.0
- v2.0.0-beta.9
- v2.0.0-beta.8
- v2.0.0-beta.7
- v2.0.0-beta.6
- v2.0.0-beta.5
- v2.0.0-beta.4
- v2.0.0-beta.3
- v2.0.0-beta.2
- v2.0.0-beta.1
- v2.0.0-alpha.14
- v2.0.0-alpha.13
- v2.0.0-alpha.12
- v2.0.0-alpha.11
- v2.0.0-alpha.10
- v2.0.0-alpha.9
- v2.0.0-alpha.8
- v2.0.0-alpha.7
- v2.0.0-alpha.6
- v2.0.0-alpha.5
- v2.0.0-alpha.4
- v2.0.0-alpha.3
- v2.0.0-alpha.2
- v2.0.0-alpha.1
- v1.1.0
- v1.0.15
- v1.0.14
- v1.0.13
- v1.0.12
- v1.0.11
- v1.0.10
- v1.0.9
- v1.0.8
- v1.0.7
- v1.0.6
- v1.0.5
- v1.0.4
- v1.0.3
- v1.0.2
- v1.0.1
- v1.0.0
This package is auto-updated.
Last update: 2024-09-19 19:17:42 UTC
README
Anax 路由模块。
一个独立的路由器,支持请求方法和动态路由匹配,从路径中提取和验证参数。
路由器将按添加顺序尝试匹配路由,并依次执行所有匹配的路由。
使用 exit()
阻止进一步匹配路由。
安装
$ composer require anax/router
用法
添加带有处理器的路由
use Anax\Route\Router; $router = new Router(); $router->add("", function () { echo "home "; }); $router->add("about", function () { echo "about "; }); $router->add("about/me", function () { echo "about/me "; }); // try it out $router->handle(""); $router->handle("about"); $router->handle("about/me"); // home about about/me
添加具有单个处理器的多个路由
通过规则数组添加共享处理器的多个路由
$router = new Router(); $router->add(["info", "about"], function () { echo "info or about - "; }); // try it out $router->handle("info"); $router->handle("about"); // info or about - info or about -
添加默认路由
此路由将匹配任何路径。
$router = new Router(); $router->always(function () { echo "always "; }); // try it out using some paths $router->handle("info"); $router->handle("about"); // always always
添加 404、403 和 500 错误处理的内部路由
添加无法匹配路由时调用的内部路由
$router = new Router(); $router->addInternal("404", function () { echo "404 "; }); $router->add("about", function () { echo "about "; }); // try it out using some paths $router->handle("whatever"); // 404
您可以添加内部路由来处理 403 和 500。这些路由将处理在路由处理器内部抛出的未捕获异常。
捕获类型为 ForbiddenException
的异常的 403 内部路由。
$router->addInternal("403", function () { echo "403 "; }); $router->add("login", function () { throw new ForbiddenException(); }); // try it out using some paths $router->handle("login"); // 403
捕获类型为 InternalErrorException
的异常的 500 内部路由。
$router->addInternal("500", function () { echo "500 "; }); $router->add("calculate", function () { throw new InternalErrorException(); }); // try it out using some paths $router->handle("calculate"); // 500
使用 * 添加子路径下任何项目的公共路由
此路由将匹配与 about/*
同级的任何项目。
$router = new Router(); $router->addInternal("404", function () { echo "404 "; }); $router->add("about/*", function () { echo "about "; }); // try it out using some paths $router->handle("about"); $router->handle("about/me"); $router->handle("about/you"); $router->handle("about/some/other"); // no match // about about about 404
使用 ** 添加子路径下任何项目的公共路由
此路由将匹配 about/**
下的任何项目,甚至是子目录。
$router = new Router(); $router->add("about/**", function () { echo "about "; }); // try it out using some paths $router->handle("about"); $router->handle("about/me"); $router->handle("about/you"); $router->handle("about/some/other"); // about about about about
部分路径作为路由处理器的参数
您可以将路由的部分发送给处理器作为参数。这使得路由处理器更加灵活和动态。
$router = new Router(); $router->addInternal("404", function () { echo "404 "; }); $router->add("about/{arg}", function ($arg) { echo "$arg "; }); ob_start(); // try it out using some paths $router->handle("about"); // not matched $router->handle("about/me"); $router->handle("about/you"); $router->handle("about/some/other"); // not matched // 404 me you 404
您还可以发送多个参数。
$router = new Router(); $router->add( "post/{year}/{month}/{day}", function ($year, $month, $day) { echo "$year-$month-$day, "; } ); // try it out using some paths $router->handle("post/2017/03/07"); $router->handle("post/1990/06/20"); // 2017-03-07, 1990-06-20,
参数类型检查
对参数应用类型检查以限制匹配的路由。
$router = new Router(); $router->addInternal("404", function () { echo "404, "; }); $router->add( "post/{year:digit}/{month:digit}/{day:digit}", function ($year, $month, $day) { echo "$year-$month-$day, "; } ); $router->add( "post/{year:digit}/{month:alpha}/{day:digit}", function ($year, $month, $day) { echo "$day $month $year, "; } ); // try it out using some paths $router->handle("post/2017/03/seven"); $router->handle("post/2017/03/07"); $router->handle("post/1990/06/20"); $router->handle("post/1990/june/20"); // 404, 2017-03-07, 1990-06-20, 20 june 1990,
类型检查支持数字、字母、字母数字和十六进制(有关详细信息,请参阅 ctype)。
按请求方法的路由
可以设置路由以仅匹配一个请求方法。
$router = new Router(); $router->any(["GET"], "about", function () { echo "GET "; }); $router->any(["POST"], "about", function () { echo "POST "; }); $router->any(["PUT"], "about", function () { echo "PUT "; }); $router->any(["DELETE"], "about", function () { echo "DELETE "; }); // try it out using some paths $router->handle("about", "GET"); $router->handle("about", "POST"); $router->handle("about", "PUT"); $router->handle("about", "DELETE"); // GET POST PUT DELETE
路由也可以匹配多个请求方法。
$router = new Router(); $router->any(["GET", "POST"], "about", function () { echo "GET+POST "; }); $router->any("PUT | DELETE", "about", function () { echo "PUT+DELETE "; }); // try it out using some paths $router->handle("about", "GET"); $router->handle("about", "POST"); $router->handle("about", "PUT"); $router->handle("about", "DELETE"); // GET+POST GET+POST PUT+DELETE PUT+DELETE
依赖关系
这些都是对其他模块的依赖关系。
许可证
此软件携带 MIT 许可证。有关详细信息,请参阅 LICENSE.txt。
.
..: Copyright (c) 2013 - 2019 Mikael Roos, mos@dbwebb.se