94 lines
3.4 KiB
PHP
94 lines
3.4 KiB
PHP
<?php
|
|
session_start();
|
|
require_once __DIR__ . '/src/MVC/autoloader.php';
|
|
|
|
use MVC\Classes\Logger;
|
|
|
|
// Global Error & Exception Handlers
|
|
set_exception_handler([Logger::class, 'logException']);
|
|
set_error_handler([Logger::class, 'logError']);
|
|
|
|
// 1. Determine the service
|
|
$service = $_GET['service'] ?? null;
|
|
|
|
// Fallback to path-based routing if no service parameter is given
|
|
if ($service === null) {
|
|
$uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
|
|
$service = trim($uri, '/');
|
|
if (empty($service)) {
|
|
$service = 'home';
|
|
}
|
|
}
|
|
|
|
try {
|
|
$router = new \MVC\Core\Router();
|
|
|
|
// Load routes from config
|
|
require_once __DIR__ . '/src/config/routes.php';
|
|
|
|
// 2. Dispatch the service
|
|
$routeInfo = $router->getRoute($service);
|
|
$output = $router->dispatch($service);
|
|
|
|
// 3. Conditional Layout Rendering
|
|
if ($routeInfo && !empty($routeInfo['isApi'])) {
|
|
// API Route: Just echo the output and exit
|
|
echo $output;
|
|
exit;
|
|
}
|
|
|
|
$layout = new \MVC\Layout\Layout('main');
|
|
$layout->setBlock('title', 'MVC Starter');
|
|
$layout->setBlock('header_title', '🚀 MVC Dashboard');
|
|
|
|
$headerActions = '<button class="btn-toggle" onclick="toggleTheme()">🌓 Theme umschalten</button>';
|
|
if (\MVC\Classes\Auth::check()) {
|
|
$user = \MVC\Classes\Auth::user();
|
|
$headerActions = '<span style="margin-right: 15px; font-size: 0.9rem;">👤 ' . htmlspecialchars($user['username']) . '</span>' . '<a href="?service=logout" class="btn-toggle" style="text-decoration: none; background: rgba(255,255,255,0.2); margin-right: 10px;">🚪 Logout</a> ' . $headerActions;
|
|
} else {
|
|
$headerActions = '<a href="?service=login" class="btn-toggle" style="text-decoration: none; background: var(--accent); color: white; margin-right: 10px;">🔑 Login</a> ' . $headerActions;
|
|
}
|
|
$layout->setBlock('header_actions', $headerActions);
|
|
|
|
// Sidebar Block
|
|
ob_start();
|
|
// Load navigation from config
|
|
$navItems = require __DIR__ . '/src/config/navigation.php';
|
|
?>
|
|
<nav>
|
|
<?php foreach ($navItems as $navKey => $item):
|
|
$isLoggedIn = \MVC\Classes\Auth::check();
|
|
$isAdmin = \MVC\Classes\Auth::isAdmin();
|
|
|
|
// Check visibility based on Router middleware
|
|
$requiresAuth = $router->hasMiddleware($navKey, 'auth');
|
|
$requiresAdmin = $router->hasMiddleware($navKey, 'admin');
|
|
|
|
if ($requiresAuth && !$isLoggedIn) continue;
|
|
if ($requiresAdmin && !$isAdmin) continue;
|
|
?>
|
|
<a href="?service=<?= $navKey ?>" class="<?= $service === $navKey ? 'active' : '' ?>"><?= $item['label'] ?></a>
|
|
<?php endforeach; ?>
|
|
|
|
<?php if (!\MVC\Classes\Auth::check()): ?>
|
|
<div style="margin-top: 20px; padding: 10px; font-size: 0.8rem; opacity: 0.5; border-top: 1px solid var(--border);">
|
|
🔒 Melden Sie sich an, um alle Funktionen zu sehen.
|
|
</div>
|
|
<?php endif; ?>
|
|
</nav>
|
|
<?php
|
|
$layout->setBlock('sidebar', ob_get_clean());
|
|
|
|
$layout->setBlock('content', $output);
|
|
echo $layout->render();
|
|
} catch (\Throwable $e) {
|
|
// Log the error globally
|
|
Logger::logException($e);
|
|
|
|
http_response_code(500);
|
|
echo "<h1>Error</h1><p>" . htmlspecialchars($e->getMessage()) . "</p>";
|
|
if (ini_get('display_errors')) {
|
|
echo "<pre>" . htmlspecialchars($e->getTraceAsString()) . "</pre>";
|
|
}
|
|
}
|