gpt4 book ai didi

slim - 具有相同签名的多个 Slim 路由

转载 作者:行者123 更新时间:2023-12-01 06:08:03 24 4
gpt4 key购买 nike

我们正在考虑使用 Slim 3 作为我们 API 的框架。我已经搜索过 SO 和 Slim 文档,但找不到问题的答案。如果我们有不同的路由文件(例如 v1、v2 等)并且如果两个路由具有相同的签名,则会抛出错误。有什么方法可以级联路由,以便使用特定签名的最后加载路由吗?

比如v1.php有一个GET("/test")的路由,v2.php也有这个路由,能不能用最新的版本?更简单的是,如果一个路由文件包含两个具有相同签名的路由,是否可以使用后一种方法(并且不会抛出错误)?

问了一个类似的问题here但这使用了钩子(Hook)(根据 here 已从 Slim 3 中删除)

最佳答案

我查看了 Slim 代码,但没有找到允许重复路由(防止异常)的简单方法。新的 Slim 使用 FastRoute作为依赖。它调用 FastRoute\simpleDispatcher 并且不提供任何配置可能性。即使它确实允许一些配置,FastRoute 也没有任何内置选项来允许重复的路由。将需要 DataGenerator 的自定义实现。

但是按照上面的说明,我们可以通过向 Slim App 传递一个自定义的 Router 来获得一个自定义的 DataGenerator,它会实例化一些 FastRoute::Dispatcher 实现 然后使用自定义 DataGenerator

首先是 CustomDataGenerator(让我们走简单的路,从 \FastRoute\RegexBasedAbstract\FastRoute\GroupCountBased 做一些复制和粘贴)

<?php
class CustomDataGenerator implements \FastRoute\DataGenerator {
/*
* 1. Copy over everything from the RegexBasedAbstract
* 2. Replace abstract methods with implementations from GroupCountBased
* 3. change the addStaticRoute and addVariableRoute
* to the following implementations
*/
private function addStaticRoute($httpMethod, $routeData, $handler) {
$routeStr = $routeData[0];

if (isset($this->methodToRegexToRoutesMap[$httpMethod])) {
foreach ($this->methodToRegexToRoutesMap[$httpMethod] as $route) {
if ($route->matches($routeStr)) {
throw new BadRouteException(sprintf(
'Static route "%s" is shadowed by previously defined variable route "%s" for method "%s"',
$routeStr, $route->regex, $httpMethod
));
}
}
}
if (isset($this->staticRoutes[$httpMethod][$routeStr])) {
unset($this->staticRoutes[$httpMethod][$routeStr]);
}
$this->staticRoutes[$httpMethod][$routeStr] = $handler;
}
private function addVariableRoute($httpMethod, $routeData, $handler) {
list($regex, $variables) = $this->buildRegexForRoute($routeData);
if (isset($this->methodToRegexToRoutesMap[$httpMethod][$regex])) {
unset($this->methodToRegexToRoutesMap[$httpMethod][$regex]);
}
$this->methodToRegexToRoutesMap[$httpMethod][$regex] = new \FastRoute\Route(
$httpMethod, $handler, $regex, $variables
);
}
}

然后是自定义Router

<?php
class CustomRouter extends \Slim\Router {
protected function createDispatcher() {
return $this->dispatcher ?: \FastRoute\simpleDispatcher(function (\FastRoute\RouteCollector $r) {
foreach ($this->getRoutes() as $route) {
$r->addRoute($route->getMethods(), $route->getPattern(), $route->getIdentifier());
}
}, [
'routeParser' => $this->routeParser,
'dataGenerator' => new CustomDataGenerator()
]);
}
}

最后使用自定义路由器实例化 Slim 应用

<?php
$app = new \Slim\App(array(
'router' => new CustomRouter()
));

上面的代码,如果检测到重复的路线,则删除以前的路线并存储新路线。

我希望我没有错过实现此结果的任何更简单的方法。

关于slim - 具有相同签名的多个 Slim 路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34897414/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com