public static function resource($path, array $options) { $options = array_replace_recursive(["class" => null, "list_mode" => "list", "sql" => ["table" => null, "raw" => null, "primary_key" => "id"]], $options); $path = rtrim($path, "/") ?: "/"; $resource = $options["class"]; $table = $options["sql"]["table"]; $raw = $options["sql"]["raw"]; $pkey = $options["sql"]["primary_key"]; $sql_list = $raw ?: "SELECT * FROM {$table}"; $sql_single = strpos($sql_list, "WHERE") === false ? "{$sql_list} WHERE {$pkey}=:{$pkey}" : str_replace("WHERE ", "WHERE {$pkey}=:{$pkey} AND ", $sql_list); $sql_single .= " LIMIT 1"; // Ensure endpoint options sanity if (class_exists($resource, false) && ($table || $raw) && $pkey) { // List Route::on($path, function () use($resource, $table, $sql_list, $options) { $resource::setExposure($options["list_mode"]); return $resource::fromSQL($sql_list); }); // Single Route::on("{$path}/:id", function ($id) use($resource, $table, $pkey, $sql_single) { return ['data' => $resource::singleFromSQL($sql_single, ["{$pkey}" => $id]) ?: API::error("Not found", 404)]; }); // Projection short-hand Route::on("{$path}/:id/:parameter", function ($id, $parameter) use($resource, $table, $pkey, $sql_single) { Filter::add("api.{$resource}.getProjectionFields", function ($t) use($parameter) { return $parameter; }); return ['data' => $resource::singleFromSQL($sql_single, ["{$pkey}" => $id]) ?: API::error("Not found", 404)]; }); } }
<?php Route::on("/phpinfo", [], function (array $args = null) : bool { return phpinfo(); });
// respond json Route::on('/portfolio/formations', [], 'formations'); Route::on('/portfolio/:lang/formations', [], 'formations'); function formations(array $args = null) : bool { $args['lang'] = $args['lang'] ?? DEFAULT_LANGUAGE; Document::mime(Document::JSON); Document::language($args['lang'], 'portfolio'); echo json_encode([['name' => NAME_ENGINEER, 'date' => DATE_ENGINEER, 'description' => DESC_ENGINEER], ['name' => NAME_CQPM, 'date' => DATE_CQPM, 'description' => DESC_CQPM], ['name' => NAME_CISCO, 'date' => DATE_CISCO, 'description' => DESC_CISCO], ['name' => NAME_BAC, 'date' => DATE_BAC, 'description' => DESC_BAC]]); return true; } // ---------------------------------------------------------------------------- // respond json Route::on('/portfolio/projects', [], 'projects'); Route::on('/portfolio/:lang/projects', [], 'projects'); function projects(array $args = null) : bool { $args['lang'] = $args['lang'] ?? DEFAULT_LANGUAGE; Document::mime(Document::JSON); Document::language($args['lang'], 'portfolio'); echo json_encode([['name' => NAME_FESTIGEEK, 'date' => DATE_FESTIGEEK, 'description' => DESC_FESTIGEEK], ['name' => NAME_PORTFOLIO, 'date' => DATE_PORTFOLIO, 'description' => DESC_PORTFOLIO], ['name' => NAME_SHELLCHOOSER, 'date' => DATE_SHELLCHOOSER, 'description' => DESC_SHELLCHOOSER], ['name' => NAME_FRAMEWORK, 'date' => DATE_FRAMEWORK, 'description' => DESC_FRAMEWORK]]); return true; } // ---------------------------------------------------------------------------- // envoie du message Route::on('/portfolio/message', [], function (array $args = null) : bool { Document::mime(Document::JSON); $send = mail('*****@*****.**', 'Portfolio - Message de ' . $args['firstname'] . ' ' . $args['lastname'], $args['mail-content']); echo json_encode(['send' => $send]); return $send; });
<?php Event::on(404, function () { Response::html(View::from('special/error', ['code' => 404, 'message' => 'Page not found'])); }); Route::on('/', function () { return View::from('index'); });