burzum/fast-route-middleware

安装: 9

依赖: 0

建议者: 0

安全: 0

星星: 2

关注者: 2

分支: 0

类型:中间件

dev-master 2019-12-05 21:33 UTC

This package is auto-updated.

Last update: 2024-09-06 08:11:41 UTC


README

Software License Scrutinizer Coverage Code Quality

一个方便且严格类型化的快速路由中间件。

如何使用它

快速路由的结果将是一个数组,其中第一个键表示结果类型。快速路由目前知道三种不同的情况

  1. 路由找到
  2. 路由未找到
  3. 路由不允许

中间件负责从快速路由获取这些结果,但您需要定义自己的处理器,因为这取决于您和您的应用程序。中间件接收一个需要实现特定于每次的接口的对象。

必须创建至少找到处理器!每种类型的处理器都必须实现相应的接口。其他两个处理器是可选的!

// 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。