Compare commits

..

4 Commits

Author SHA1 Message Date
2a439b8df4 add docker support :3 2026-04-21 15:00:03 +02:00
7ee3d6441a fixing stuff 2026-04-21 14:03:37 +02:00
3de979fc65 owowowo 2026-04-21 13:56:05 +02:00
894cc2d3fd little stash... 2026-04-21 13:43:08 +02:00
10 changed files with 164 additions and 0 deletions

11
Dockerfile Normal file
View File

@@ -0,0 +1,11 @@
FROM php:8.3-apache
RUN a2enmod rewrite
COPY . /var/www/html
RUN sed -i 's|/var/www/html|/var/www/html/public|g' \
/etc/apache2/sites-available/000-default.conf
RUN sed -i 's|AllowOverride None|AllowOverride All|g' \
/etc/apache2/apache2.conf

4
config/routes.php Normal file
View File

@@ -0,0 +1,4 @@
<?php
use src\Controller\HomeController;
$router->get('/', [new HomeController(), 'index']);

4
public/.htaccess Normal file
View File

@@ -0,0 +1,4 @@
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]

13
public/index.php Normal file
View File

@@ -0,0 +1,13 @@
<?php
require_once __DIR__ . '/../src/Autoloader.php';
use src\Router;
$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;
}
}
}
}

View File

@@ -0,0 +1,34 @@
<?php
namespace src\Controller;
abstract class BaseController
{
protected function render(string $view, array $data = []): void
{
extract($data);
$viewPath = __DIR__ . '/../../templates/' . $view . '.php';
if (!file_exists($viewPath)) {
http_response_code(500);
echo "View '{$view}' nicht gefunden.";
return;
}
require $viewPath;
}
protected function redirect(string $url): void
{
header('Location: ' . $url);
exit;
}
protected function json(mixed $data, int $status = 200): void
{
http_response_code($status);
header('Content-Type: application/json');
echo json_encode($data);
exit;
}
}

View File

@@ -0,0 +1,12 @@
<?php
namespace src\Controller;
class HomeController extends BaseController
{
public function index(): void
{
$this->render('home', [
'title' => 'meow :3'
]);
}
}

31
src/Router.php Normal file
View File

@@ -0,0 +1,31 @@
<?php
namespace src;
class Router
{
private array $routes = [];
public function get(string $path, callable $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
{
$path = parse_url($uri, PHP_URL_PATH) ?? '/';
$handler = $this->routes[$method][$path] ?? null;
if ($handler === null) {
http_response_code(404);
echo '404 Not Found';
return;
}
call_user_func($handler);
}
}

10
templates/home.php Normal file
View File

@@ -0,0 +1,10 @@
<?php
ob_start();
?>
<h1><?= htmlspecialchars($title) ?></h1>
<p>Willkommen auf meiner Seite!</p>
<?php
$content = ob_get_clean();
require __DIR__ . '/layout.php';

10
templates/layout.php Normal file
View File

@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<title><?= htmlspecialchars($title) ?></title>
</head>
<body>
<?= $content ?>
</body>
</html>