kambo / router
Kambo 路由器
v0.9
2017-07-06 17:10 UTC
Requires
- php: >=7.0
- kambo/enum: dev-master
- psr/http-message: *
Requires (Dev)
- phpunit/phpunit: 6.1.*
This package is auto-updated.
Last update: 2024-09-19 16:25:54 UTC
README
这只是另一个具有以下特点的PHP路由器
- 支持PSR-7 - HTTP消息接口
- 支持闭包和控制器/模块的两种调度器
- 即使没有mod_rewrite也可以使用
安装
安装库的首选方法是使用composer
composer require kambo/router
用法
URL重写
要在启用mod_rewrite的Apache中启用重写支持,请在其根目录中创建一个名为 .htaccess 的文件,并使用以下设置
Options +FollowSymLinks RewriteEngine On RewriteRule ^(.*)$ index.php [NC,L]
要禁用mod_rewrite支持,请使用setUrlFormat方法
$matcher->setUrlFormat(RouteMode::GET_FORMAT);
路由定义
通过在RouteCollection实例上调用createRoute()来添加路由
$routeCollection->createRoute($method, $routePattern, $handler);
$method
是HTTP方法名称,表示Kambo\Router\Enum\Method枚举中的值,用于匹配特定路由,例如:Method::GET
默认情况下,$routePattern
使用一种语法,其中{foo}
指定一个名为foo
的占位符,并匹配正则表达式[^/]+
。要调整占位符匹配的模式,您可以通过编写{bar:[0-9]+}
来指定自定义模式。以下是一些示例
// Matches /user/kambo/123, but not /user/kambo/abc $routeCollection->addRoute(Method::GET, '/user/{name}/{id:\d+}', $handler);
也可以使用快捷方式方法为所有方法使用
// Shortcut for createRoute(Method::GET, '/user/{name}/{id:\d+}', $handler); $routeCollection->get('/user/{name}/{id:\d+}', $handler) $routeCollection->post('post/url', $handler) $routeCollection->delete('delete/url', $handler) $routeCollection->put('put/url', $handler) $routeCollection->any('any/url', $handler)
也可以使用闭包作为$handler
$routeCollection->get('/article/{id:\d+}', function($id) { echo $id; });
PSR-7 - HTTP消息接口
Kambo路由器使用PSR 7兼容的请求对象实例进行抽象,以覆盖服务器变量。可以使用任何实现PSR-7的第三方库,例如Kambo/HttpMessage
路由调度器
路由器包含以下调度器
- 闭包调度器具有自动路径 <=> 闭包变量绑定功能。
- 具有强制您将代码组织为模块/控制器类结构的观点类调度器。
使用闭包调度器
<?php // Kambo\Router use Kambo\Router\Route\Collection; use Kambo\Router\Route\Builder\Base; use Kambo\Router\Dispatcher\ClosureAutoBind; use Kambo\Router\Router; use Kambo\Router\Matcher\Regex; // Kambo\Http\Message use Kambo\Http\Message\Environment\Environment; use Kambo\Http\Message\Factories\Environment\ServerRequestFactory; $routeCollection = new Collection(new Base()); // Matches http://{domain}/user/{string}/transaction/{integer number} eg.: http://router-example.vg/user/kambo/transaction/1 $routeCollection->get('/user/{name}/transaction/{id:\d+}', function(int $id, string $name) { echo $id.' '.$name; }); // Matches http://{domain}/article/{integer number} eg.: http://router-example.vg/article/42 $routeCollection->get('/article/{id:\d+}', function(int $id) { echo 'article id: '.$id; }); // Create instance of the closure dispatcher with function properties auto bind functionality $dispatcherClosureAutoBind = new ClosureAutoBind(); // Create instance of the route matcher based on regular expressions $matcherRegex = new Regex($routeCollection); // Create instance of the Router $router = new Router($dispatcherClosureAutoBind, $matcherRegex); // Create Environment object based on server variables. $environment = new Environment($_SERVER, fopen('php://input', 'w+'), $_POST, $_COOKIE, $_FILES); // Create instance of ServerRequest object in this example we are using Kambo/HttpMessage (https://github.com/kambo-1st/HttpMessage) // but any other implementation of PSR 7 server request can be used. $request = (new ServerRequestFactory())->create($environment); // Start URL matching a PSR 7 compatible object must be provided $router->dispatch($request);
此示例将定义两个路由
http://{domain}/user/{string}/transaction/{integer number} http://{domain}/article/{integer number}
许可
MIT许可证(MIT),https://open-source.org.cn/licenses/MIT