diff --git a/config/routes.php b/config/routes.php new file mode 100644 index 0000000..c3653c5 --- /dev/null +++ b/config/routes.php @@ -0,0 +1,4 @@ +get('/', function () { + require_once __DIR__ . '/../templates/base.php'; +}); \ No newline at end of file diff --git a/public/index.php b/public/index.php new file mode 100644 index 0000000..f08ff53 --- /dev/null +++ b/public/index.php @@ -0,0 +1,11 @@ +addNamespace('src', __DIR__ . '/../src'); +$autoloader->register(); + +$router = new Router(); +require_once __DIR__ . '/../config/routes.php'; + +$router->dispatch($_SERVER['REQUEST_METHOD'], $_SERVER['REQUEST_URI']); \ No newline at end of file diff --git a/src/Autoloader.php b/src/Autoloader.php new file mode 100644 index 0000000..798d2c4 --- /dev/null +++ b/src/Autoloader.php @@ -0,0 +1,35 @@ +prefixes[$prefix] = $baseDir; + } + + public function register(): void + { + spl_autoload_register([$this, 'loadClass']); + } + + private function loadClass(string $class): void + { + foreach ($this->prefixes as $prefix => $baseDir) { + if (!str_starts_with($class, $prefix)) { + continue; + } + + $relative = substr($class, strlen($prefix)); + $file = $baseDir . str_replace('\\', '/', $relative) . '.php'; + + if (file_exists($file)) { + require $file; + return; + } + } + } +} \ No newline at end of file diff --git a/src/Router.php b/src/Router.php new file mode 100644 index 0000000..b626868 --- /dev/null +++ b/src/Router.php @@ -0,0 +1,30 @@ +routes['GET'][$path] = $handler; + } + + public function post(string $path, callable $handler): void + { + $this->routes['POST'][$path] = $handler; + } + + public function dispatch(string $method, string $uri): void + { + $handler = $this->routes[$method][$uri] ?? null; + + if ($handler === null) { + http_response_code(404); + echo '404 Not Found'; + return; + } + + call_user_func($handler); + } +} \ No newline at end of file diff --git a/templates/base.php b/templates/base.php new file mode 100644 index 0000000..bba7f56 --- /dev/null +++ b/templates/base.php @@ -0,0 +1,10 @@ + +
This is the homepage.
+ + + + + +