idiosyncratic / http-router
基于PSR-15的轻量级HTTP路由器实现
0.5.1
2019-11-30 05:42 UTC
Requires
- php: >=7.3
- idiosyncratic/http-exceptions: ^0.9
- psr/container: ^1.0
- psr/http-message: ^1.0
- psr/http-server-handler: ^1.0
Requires (Dev)
- idiosyncratic/devtools: ^0.2
- nikic/fast-route: ^1.3
Suggests
- nikic/fast-route: For using the bundled RouteCollection implementation
This package is auto-updated.
Last update: 2024-08-29 05:18:40 UTC
README
轻量级PSR-15 HTTP路由器实现。
安装
使用Composer
composer require idiosyncratic/http-router
使用方法
Idiosyncratic\Http\Router\Router
类实现了PSR-15的Psr\Http\Server\RequestHandlerInterface
接口,是主要类。它有两个依赖项
Idiosyncratic\Http\Router\RouteCollection
的实现,一个包含路由的集合,实现了一个方法/** * @throws Idiosyncratic\Http\Exception\Client\NotFound * @throws Idiosyncratic\Http\Exception\Client\MethodNotAllowed */ public function findRoute(ServerRequestInterface $request) : Idiosyncratic\Http\Router\Route;
Psr\Container\ContainerInterface
,负责获取匹配路由的处理程序。
还包括Idiosyncratic\Http\Router\RouteGroup
,这是基于FastRoute的RouteCollection
接口的基本实现。定义路由的接口几乎与FastRoute的完全相同,但有两大例外
RouteGroup::addRoute
的参数顺序不同。路由方法最后作为字符串参数定义。- 路由处理程序必须是实现了
Psr\Http\Server\RequestHandlerInterface
接口的类的名称。
库的基本用法(使用PHP League Container)
$container = new League\Container\Container(); $container->add(ServerRequestInterfaceImplementation::class); $routes = new Idiosyncratic\Http\Router\RouteGroup(); $routes->addRoute('/hello', ServerRequestInterfaceImplementation::class, 'GET', 'POST'); $router = new Idiosyncratic\Http\Router\Router($routes, $container); // Create instance of Psr\Http\Message\ServerRequestInterface... $response = $router->handle($serverRequest);