public function dispatch() { // Determine clean path $path = strtolower($_SERVER['REQUEST_URI']); $path = preg_replace('/^' . preg_replace('/\\//', '\\/', $this->baseurl) . '/', '', $path); // determine resource exists // TODO make more complex / correct // if (! in_array($path, $this->resources)) { // self::error(404); // } if (!$this->hasResource($path)) { Response::error(404); } // split url into resource chunks // split chunks into resource name and params // on multiple chunks, combine chunk names // combine params in single param array // call function // determine request verb and if verb is allowed $request = Request::verb(); // determine if security is required // TODO later stage. In core or in plugin? // try to route to function // $function = ltrim(rtrim($path, '/'), '/').ucfirst($request); if ($request == 'head') { $function = $this->resource . 'Get'; } else { $function = $this->resource . ucfirst($request); } if (!function_exists($function)) { Response::error(501); } else { if (count($this->params) > 0) { $function($this->params); } else { $function(); } } Response::error(500); }
public static function send($content = null, $type = 'json', $code = 200) { header('HTTP/1.1 ' . $code . ' ' . self::$ERRORS[$code]); header('X-Powered-By: Rakko'); self::cors(); if ($content === null) { $type = 'plain'; $content = self::$ERRORS[$code]; } if ($type === 'json') { $content = json_encode($content, JSON_PRETTY_PRINT); header('Content-type: application/json'); } if ($type === 'plain') { header('Content-type: text/plain'); } header('Content-Length: ' . strlen($content)); if (Request::verb() != 'head') { echo $content; } exit; }