private static function run($uri, $controller, $vars = []) { $sltCt = Route::splitUri(); $slt = Route::splitUri($uri); if (count($slt) != count($sltCt)) { return false; } for ($i = 0; $i < count($sltCt); $i++) { if (preg_match("/^{+.+}\$/", $slt[$i])) { $temp = substr($slt[$i], 1, -1); if (isset($vars[$temp]) && preg_match($vars[$temp], $sltCt[$i])) { $vars[$temp] = $sltCt[$i]; } else { return false; } } else { if ($slt[$i] != $sltCt[$i]) { return false; } } } Route::$status = Route::call($controller, $vars); }
/** * Dispatch the URI to the matched route. * * @return void */ public function dispatch() { $routesList = \Route::getRoutesList(); foreach ($routesList as $key => $value) { $routeWithPattern = str_replace("{id}", "[0-9]+", $key); $routeWithPattern = str_replace("{name}", "[a-zA-Z_ ]+", $routeWithPattern); $routeWithPattern = str_replace('/', '\\/', ltrim($routeWithPattern, '/')); $regex = '/\\/' . $routeWithPattern . '(\\/|)$/'; if (preg_match($regex, $this->routeName, $matches)) { if ($matches[0] == $this->routeName) { $matchedRouteName = $key; } } } if (isset($matchedRouteName) && in_array($this->httpMethod, $routesList[$matchedRouteName]['verbs']) && array_key_exists($matchedRouteName, $routesList)) { $this->setParams($matchedRouteName); \Route::call($matchedRouteName); } elseif (isset($matchedRouteName) && !in_array($this->httpMethod, $routesList[$matchedRouteName]['verbs']) && array_key_exists($matchedRouteName, $routesList)) { throw new MethodNotAllowedException('405 Method Not Allowed'); } else { throw new NotFoundException('404 Not Found'); } }