Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 26 additions & 7 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,31 @@

use src\core\Route;

Route::add("GET", "/webskills/", "MainController::goToMain");
Route::add("GET", "/webskills/main", "MainController::main");
Route::add("GET", "/webskills/src/user/account/login", "UserController::getLogin");
Route::add("GET", "/webskills/src/user/account/register", "UserController::getRegister");
function test () {
echo "test";
};

Route::add("POST", "/webskills/src/user/account/login", "UserController::postLogin");
Route::add("POST", "/webskills/src/user/account/register", "UserController::postRegister");
function test2 () {
echo "test2";
};

Route::run();

function test3 ($request) {
echo "<pre>";
print_r($request);
echo "</pre>";
};

Route::add("GET", "/", "test");
Route::add("GET", "/test", "test");
Route::add("GET", "/test2", "test2");
Route::add("GET", "/test3/([0-9]+)", "test3");
//Route::add("GET", "/webskills/", "MainController::goToMain");
//Route::add("GET", "/webskills/main", "MainController::main");
//Route::add("GET", "/webskills/src/user/account/login", "UserController::getLogin");
//Route::add("GET", "/webskills/src/user/account/register", "UserController::getRegister");
//
//Route::add("POST", "/webskills/src/user/account/login", "UserController::postLogin");
//Route::add("POST", "/webskills/src/user/account/register", "UserController::postRegister");

Route::run();
8 changes: 8 additions & 0 deletions router.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php
// php built-in server로 실행시킬 때 사용하는 Router
if (!preg_match("/^([^.]+)$/", $_SERVER['REQUEST_URI'])) {
return false;
}

$_GET['url'] = $_SERVER['REQUEST_URI'];
include_once("./index.php");
33 changes: 17 additions & 16 deletions src/core/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,24 @@
namespace src\core;

class Route{
private static $routes = [];
private static array $routes = [];

public static function add($method, $path, $callback) {
self::$routes[] = compact("method", "path", "callback");
}
public static function add($method, $uri, $callback) {
$uriReg = str_replace("/", "\/", $uri ?? '/');
$uriReg = "/^{$uriReg}$/";
self::$routes[] = compact("method", "uriReg", "callback");
}

public static function run() {
return call_user_func(
current(
array_filter(self::$routes, function($route) {return self::match($route["method"], $route["path"]);})
)["callback"]
);
}
public static function run() {
$route = current(array_filter(self::$routes, fn($route) => self::match($route)));
preg_match_all($route['uriReg'], $_SERVER['REQUEST_URI'], $params, 0);
$request = ['params' => $params];
return call_user_func($route["callback"], $request);
}

private static function match($method, $path) {
return $method === $_SERVER["REQUEST_METHOD"] && $path === $_SERVER["REQUEST_URI"] ?? "/";
}
};
private static function match($route) {
return $route['method'] === $_SERVER["REQUEST_METHOD"] &&
preg_match($route['uriReg'], $_SERVER["REQUEST_URI"]);
}

?>
}