3

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

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

此处提出了类似的问题,但这使用了钩子(根据此处已从 Slim 3 中删除)

4

1 回答 1

3

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

但是按照上面的说明,我们可以DataGenerator通过向 Slim App 传递一个 custom 来获得一个 custom,该 customRouter实例化一些FastRoute::Dispatcher implementation然后使用 custom DataGenerator

首先(让我们走简单的方法,从\FastRoute\RegexBasedAbstract\FastRoute\GroupCountBasedCustomDataGenerator做一些复制和粘贴)

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

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 应用

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

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

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

于 2016-01-21T15:04:24.103 回答