burzum / fast-route-middleware
dev-master
2019-12-05 21:33 UTC
Requires
- php: ~7.1
- nikic/fast-route: ^1.3
- psr/http-message: ^1.0
- psr/http-server-handler: ^1.0
- psr/http-server-middleware: ^1.0
Requires (Dev)
- phpstan/phpstan: ^0.12.0
- phpunit/phpunit: ^8.0
- squizlabs/php_codesniffer: ^3.1
This package is auto-updated.
Last update: 2024-09-06 08:11:41 UTC
README
一个方便且严格类型化的快速路由中间件。
如何使用它
快速路由的结果将是一个数组,其中第一个键表示结果类型。快速路由目前知道三种不同的情况
- 路由找到
- 路由未找到
- 路由不允许
中间件负责从快速路由获取这些结果,但您需要定义自己的处理器,因为这取决于您和您的应用程序。中间件接收一个需要实现特定于每次的接口的对象。
您必须创建至少找到处理器!每种类型的处理器都必须实现相应的接口。其他两个处理器是可选的!
// Route was found and matched the URL class MyFoundHandler implements FoundHandlerInterface { public function handle(ServerRequestInterface $request, $handler, array $vars): ?ResponseInterface { // Handle the request and return null or a response object // Dispatch your controllers or request handlers here based on the route vars } } // Route was not found, URL didn't match class MyNotFoundHandler implements NotFoundHandlerInterface { public function handle(ServerRequestInterface $request): ?ResponseInterface { // Handle the request and return null or a response object // Dispatch your controllers or request handlers here } } // Route was found but is not allowed to be accessible class MyNotAllowedHandler implements NotAllowedHandlerInterface { public function handle(ServerRequestInterface $request, array $notAllowedMethods): ?ResponseInterface { // Handle the request and return null or a response object // Dispatch your controllers or request handlers here } }
然后配置中间件。您必须传递一个FoundHandler,其他两个是可选的!
请查看 FastRoute 文档 了解如何配置FastRoutes调度程序及其路由。
$dispatcher = SimpleDispatcher(function(RouteCollector $r) { // Your routes... }); $fastRouteMiddleware = new FastRouteMiddleware( $dispatcher, new MyFoundHandler(), new MyNotFoundHandler(), new MyNotAllowedHandler() ); // Pass the middleware to your middleware handler implementation
许可证
MIT许可证
版权所有 (c) 2018 by Florian Krämer。