81 lines
2.3 KiB
PHP
81 lines
2.3 KiB
PHP
<?php
|
|
|
|
require_once("../config/config.php");
|
|
require_once("../libs/Database.php");
|
|
require_once("../libs/Factory.php");
|
|
require_once("../libs/Model.php");
|
|
require_once("../libs/Controller.php");
|
|
|
|
|
|
if (session_status() === PHP_SESSION_NONE) {
|
|
session_start();
|
|
}
|
|
|
|
// ================= AUTLOADER =================
|
|
spl_autoload_register(function ($class) {
|
|
$paths = [
|
|
__DIR__ . '/../controller/' . $class . '.php',
|
|
__DIR__ . '/../model/' . $class . '.php',
|
|
__DIR__ . '/../view/' . $class . '.php'
|
|
];
|
|
|
|
foreach ($paths as $file) {
|
|
if (file_exists($file)) {
|
|
require_once $file;
|
|
return;
|
|
}
|
|
}
|
|
});
|
|
|
|
// ================= ROUTING =================
|
|
$url = $_GET['url'] ?? '';
|
|
$url = trim($url, '/');
|
|
$parts = explode('/', $url);
|
|
|
|
// --- Controller oder Datei bestimmen ---
|
|
$controllerBase = !empty($parts[0]) ? $parts[0] : 'Home'; // Default Home
|
|
$method = !empty($parts[1]) ? $parts[1] : 'index';
|
|
|
|
// Prüfen: existiert Datei im Controller-Ordner?
|
|
$controllerFile = __DIR__ . '/../controller/' . $controllerBase . '.php';
|
|
|
|
try {
|
|
if (file_exists($controllerFile)) {
|
|
// Datei existiert → einbinden
|
|
require_once $controllerFile;
|
|
|
|
// Prüfen, ob Klasse existiert
|
|
if (class_exists($controllerBase)) {
|
|
$controller = new $controllerBase();
|
|
} else {
|
|
// Falls Klasse nicht existiert → anonymes Objekt
|
|
$controller = new class {
|
|
public function __call($name, $args) {
|
|
throw new Exception("Methode '$name' nicht gefunden.");
|
|
}
|
|
};
|
|
}
|
|
|
|
// Prüfen, ob Methode existiert
|
|
if (!method_exists($controller, $method)) {
|
|
throw new Exception("Methode '$method' nicht gefunden");
|
|
}
|
|
|
|
// Methode ausführen
|
|
$controller->$method();
|
|
|
|
} else {
|
|
// Datei existiert nicht → ErrorController
|
|
throw new Exception("Controller-Datei '$controllerBase.php' nicht gefunden");
|
|
}
|
|
|
|
} catch (Exception $e) {
|
|
// ================= ERROR FALLBACK =================
|
|
if (class_exists('ErrorController')) {
|
|
$error = new ErrorController();
|
|
$error->index($e->getMessage());
|
|
} else {
|
|
echo "Fehler: " . $e->getMessage();
|
|
}
|
|
}
|