public static function createFromGlobals() { $request = new Request(); $request->set("method", strtolower($_SERVER["REQUEST_METHOD"])); $request->set("post", array_key_exists("form", $_POST) ? $_POST["form"] : array()); $path = $_SERVER["REQUEST_URI"]; $base = $_SERVER["BASE"]; while ($base[0] == $path[0]) { $path = substr($path, 1); $base = substr($base, 1); } $path = str_replace("?", "", $path); $request->set("path", $path); return $request; }
public function match(Request $request) { $path = PATH_CONFIG . "/routing.yml"; $content = file_get_contents($path); if ($content === false) { throw new ApplicationException("try to open unexisting file: " . $path); } $this->routes = Yaml::parse($content); $response = new Response(); foreach ($this->routes as $name => $args) { $parameters = self::test($args["path"], $request->get("path")); if ($parameters) { if (!array_key_exists("method", $args) || in_array($request->get("method"), $args["method"])) { $response->setCallback($args["controller"], $args["action"]); $request->set("parameters", $parameters); $this->get("logger")->info($this, "route matched: " . $name); return $response; } } } throw new WrongRouteException("route not found: " . $request->get("path")); }