From e8f5015a8e29bad01e6236696c37abd6592237ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Mon, 7 Sep 2020 10:42:10 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- HttpServer/Route/Router.php | 39 +++++++++++++++++++++++++++---------- 1 file changed, 29 insertions(+), 10 deletions(-) diff --git a/HttpServer/Route/Router.php b/HttpServer/Route/Router.php index 655aaca3..d5244a15 100644 --- a/HttpServer/Route/Router.php +++ b/HttpServer/Route/Router.php @@ -49,16 +49,25 @@ class Router extends Application implements RouterInterface if (!isset($this->nodes[$method])) { $this->nodes[$method] = []; } - list($first, $explode) = $this->split($path); - $parent = $this->nodes[$method][$first] ?? null; - if (empty($parent)) { - $parent = $this->NodeInstance($first, 0, $method); - $this->nodes[$method][$first] = $parent; - } - if ($first !== '/') { - $parent = $this->bindNode($parent, $explode, $method); - } - return $parent->bindHandler($handler); +// list($first, $explode) = $this->split($path); + + $paths = array_column($this->groupTacks, 'prefix'); + $path = implode('/', $paths) . '/' . ltrim($path, '/'); + + +// $parent = $this->nodes[$method][$first] ?? null; + + $this->nodes[$method][$path] = $this->NodeInstance($path, 0, $method); + + +// if (empty($parent)) { +// $parent = $this->NodeInstance($first, 0, $method); +// $this->nodes[$method][$first] = $parent; +// } +// if ($first !== '/') { +// $parent = $this->bindNode($parent, $explode, $method); +// } + return $this->nodes[$method][$path]->bindHandler($handler); } /** @@ -411,6 +420,16 @@ class Router extends Application implements RouterInterface */ private function find_path($request) { + $method = $request->getMethod(); + if (!isset($this->nodes[$method])) { + return null; + } + $methods = $this->nodes[$method]; + $uri = implode('/', $request->getExplode()); + if (!isset($methods[$uri])) { + return null; + } + return $this->nodes[$method][$uri]; $node = $this->tree_search($request->getExplode(), $request->getMethod()); if ($node instanceof Node) { return $node;