little stash...
This commit is contained in:
4
config/routes.php
Normal file
4
config/routes.php
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
<?php
|
||||||
|
$router->get('/', function () {
|
||||||
|
require_once __DIR__ . '/../templates/base.php';
|
||||||
|
});
|
||||||
11
public/index.php
Normal file
11
public/index.php
Normal 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
35
src/Autoloader.php
Normal 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
30
src/Router.php
Normal 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
10
templates/base.php
Normal 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
11
templates/layout.php
Normal 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>
|
||||||
Reference in New Issue
Block a user