apex / router
轻量级,直观的路由器
1.0.1
2024-01-11 16:34 UTC
Requires
- php: >=8.1
- apex/container: ^2.0
- apex/syrus: ^2.0
- nyholm/psr7: ^1.5
- nyholm/psr7-server: ^1.0
- psr/container: ^2.0
- psr/http-server-handler: ^1.0
- psr/http-server-middleware: ^1.0
- symfony/yaml: ^6.2
Requires (Dev)
- apex/signer: ^2.0
- phpunit/phpunit: ^9.5
Suggests
- apex/debugger: ^2.0
README
这是一个轻量级、易于理解和使用的HTTP路由器,它还完全可选地集成了Syrus模板引擎,使其本质上成为一个小巧的微框架。它不适合做重负载,但如果您只需要一个结构良好且快速运行的解决方案,它将是极好的。支持以下功能:
- 使用YAML文件定义路由,支持部分和完整匹配路径、动态路径参数和多个主机名。
- 完全支持PSR15和PSR7,并使用中间件类来转发所有HTTP请求。
- 所有中间件类都完全支持通过Apex容器进行属性和构造函数依赖注入。
- 可选集成Syrus模板引擎,允许自动路由/映射模板,每个模板一个单独的PHP文件,每个PHP类中包含特定于HTTP方法的PHP函数。
安装
使用Composer安装:
composer require apex/router
目录
基本用法
对于快速示例,请查看此包附带example.php脚本。
首先,将一些路由添加到您的YAML文件中
routes:
default: Syrus
members: MembersArea
contact$: ContactPage
"product/:category/:product_id": PathParamsExample
use Apex\Router\Router; use Nyholm\Psr7Server\ServerRequestCreator; use Nyholm\Psr7\Factory\Psr17Factory; use League\Uri\Http; require_once("./vendor/autoload.php"); // Define a uri to test with $uri = Http::createFromString("http://example.com/category/cars"); // Generate PSR7 compliant server request object $factory = new Psr17Factory(); $creator = new ServerRequestCreator($factory, $factory, $factory, $factory); $request = $creator->fromGlobals()->withUri($uri); // Handle http request via router $router = new Router(); $response = $router->handle($request); // If this is handled via Syrus auto-routing, the // HTML would have already been output to the user. // Otherwise, $response is a PSR7 ResponseInterface object. // Alternatively, we can use the lookup method $response = $router->lookup($request); // This simply determines the route, obtains any dynamic path parameters, // and instantiates the middleware, but does not execute the process() method within it. // Instead, $response is a Apex\Router\RouterResponse instance. // Now, to execute the middle wear and output: $router->execute($response, true);
支持
如果您有任何问题、问题或反馈,请随时在ApexPl Reddit子版块上留言,以获得快速而有帮助的回应。