/** * Find route in route tree structure. * * @param $method * @return array * @throws \Exception */ public function getURI($method = null) { //$uri = "/".substr($_SERVER['PHP_SELF'], strlen($_SERVER['SCRIPT_NAME']) + 1)."/"; $uri = $_SERVER['REQUEST_URI']; if ($this->routesTree == null) { $this->routesTree = $this->parseRoutes(self::$rawRoutes); } $search = $this->normalize($uri); $node = $this->routesTree; $params = []; //loop every segment in request url, compare it, collect parameters names and values foreach ($search as $v) { if (isset($node[$v['use']])) { $node = $node[$v['use']]; } elseif (isset($node['*'])) { $node = $node['*']; $params[$node['name']] = $v['name']; } elseif (isset($node['?'])) { $node = $node['?']; $params[$node['name']] = $v['name']; } else { foreach (self::$rawRoutes as $route) { if ($node['exec']['method'][$method] == array_values($route['method'])[0]) { throw new \Exception('Route for uri: ' . $uri . ' was not found'); } } } } //check for route with optional parameters that are not in request url until valid action is found while (!isset($node['exec']) && isset($node['?'])) { $node = $node['?']; } if (isset($node['exec'])) { if (!isset($node['exec']['method'][$method]) && !isset($node['exec']['method']['any'])) { throw new \Exception('Method: ' . $method . ' is not allowed for this route'); } $this->controller = Common::str_lreplace('Controller', '', explode("@", $node['exec']['method'][$method])[0]); $this->method = explode("@", $node['exec']['method'][$method])[1]; $this->params = $params; } $this->row = self::$rawRoutes; //throw new \Exception('Route for uri: ' . $uri . ' was not found'); }