little stash...

This commit is contained in:
2026-04-21 13:43:08 +02:00
parent e3fd36d302
commit 894cc2d3fd
6 changed files with 101 additions and 0 deletions

4
config/routes.php Normal file
View File

@@ -0,0 +1,4 @@
<?php
$router->get('/', function () {
require_once __DIR__ . '/../templates/base.php';
});

11
public/index.php Normal file
View File

@@ -0,0 +1,11 @@
<?php
require_once __DIR__ . '/../src/Autoloader.php';
$autoloader = new Autoloader();
$autoloader->addNamespace('src', __DIR__ . '/../src');
$autoloader->register();
$router = new Router();
require_once __DIR__ . '/../config/routes.php';
$router->dispatch($_SERVER['REQUEST_METHOD'], $_SERVER['REQUEST_URI']);

35
src/Autoloader.php Normal file
View File

@@ -0,0 +1,35 @@
<?php
class Autoloader
{
private array $prefixes = [];
public function addNamespace(string $prefix, string $baseDir): void
{
$prefix = trim($prefix, '\\') . '\\';
$baseDir = rtrim($baseDir, '/') . '/';
$this->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;
}
}
}
}

30
src/Router.php Normal file
View File

@@ -0,0 +1,30 @@
<?php
namespace src
public class Router
{
private array $routes = [];
public function get(string $path, cllable $handler): void
{
$this->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);
}
}

10
templates/base.php Normal file
View File

@@ -0,0 +1,10 @@
<?php
$title = 'Home';
ob_start();
?>
<h1>Welcome</h1>
<p>This is the homepage.</p>
<?php
$content = ob_get_clean();
require_once __DIR__ . '/layout.php';

11
templates/layout.php Normal file
View File

@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><?= $title ?? 'Acme' ?></title>
</head>
<body>
<?= $content ?>
</body>
</html>