Compare commits
6 Commits
e3fd36d302
...
thehe
| Author | SHA1 | Date | |
|---|---|---|---|
| 702803d982 | |||
| b86d6129c3 | |||
| 2a439b8df4 | |||
| 7ee3d6441a | |||
| 3de979fc65 | |||
| 894cc2d3fd |
11
Dockerfile
Normal file
11
Dockerfile
Normal 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
4
config/routes.php
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
<?php
|
||||||
|
use src\Controller\HomeController;
|
||||||
|
|
||||||
|
$router->get('/', [new HomeController(), 'index']);
|
||||||
4
public/.htaccess
Normal file
4
public/.htaccess
Normal 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
13
public/index.php
Normal 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
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
38
src/Controller/BaseController.php
Normal file
38
src/Controller/BaseController.php
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
<?php
|
||||||
|
namespace src\Controller;
|
||||||
|
|
||||||
|
abstract class BaseController
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
protected string $templatePath = __DIR__ . '/../../templates/'
|
||||||
|
) {}
|
||||||
|
|
||||||
|
protected function render(string $view, array $data = []): void
|
||||||
|
{
|
||||||
|
extract($data);
|
||||||
|
|
||||||
|
$viewPath = $this->templatePath . '/' . $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;
|
||||||
|
}
|
||||||
|
}
|
||||||
12
src/Controller/HomeController.php
Normal file
12
src/Controller/HomeController.php
Normal 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
31
src/Router.php
Normal 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
10
templates/home.php
Normal 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
10
templates/layout.php
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="de">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<title><?= htmlspecialchars($title) ?></title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<?= $content ?>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
Reference in New Issue
Block a user