public function bindRequest(Request $request) { $get = function (&$var) { return $var; }; foreach ($this->fields as $key => $value) { $this->bind($key, $get($request->get("post")[$key])); } }
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")); }
<?php use Aku\Core\Controller\Kernel; use Aku\Core\Model\Request; define("PATH_ROOT", dirname(__DIR__)); define("PATH_CONFIG", dirname(__DIR__) . "/config"); define("PATH_BASE", $_SERVER["BASE"]); require_once PATH_ROOT . "/vendor/autoload.php"; $kernel = new Kernel(); $request = Request::createFromGlobals(); $response = $kernel->handle($request); $response->send();