add documentaation
This commit is contained in:
1 parent
307819681e
commit
7a2d53d7b1
1 file changed
+226
-16
@@ -1,7 +1,7 @@
|
||||
<?php
|
||||
/**
|
||||
* Ultimate MVC Project Bootstrapper (Fixed Version with Header)
|
||||
* Erstellt ein voll funktionsfähiges Projekt inkl. Dev-Server, Header, DarkMode & freiem Zugriff.
|
||||
* Ultimate MVC Project Bootstrapper (Fixed Version with Header & Auth)
|
||||
* Erstellt ein voll funktionsfähiges Projekt inkl. Dev-Server, Header, DarkMode & Auth-System.
|
||||
*/
|
||||
|
||||
if (php_sapi_name() !== 'cli') {
|
||||
@@ -730,7 +730,7 @@ class Router {
|
||||
|
||||
if ($middleware === 'auth') {
|
||||
if (empty($_SESSION['user_logged_in'])) {
|
||||
header('Location: login.php');
|
||||
header('Location: ?service=login');
|
||||
exit;
|
||||
}
|
||||
}
|
||||
@@ -815,7 +815,7 @@ $files['WEBROOT/src/MVC/Layout/Templates/main.php'] = <<<'HTML'
|
||||
.btn-toggle:hover { background: rgba(255,255,255,0.1); }
|
||||
nav a { display: block; padding: 12px; text-decoration: none; color: inherit; border-radius: 5px; margin-bottom: 5px; }
|
||||
nav a:hover { background: rgba(0,0,0,0.05); }
|
||||
nav a.active { background: rgba(0,0,0,0.05); }
|
||||
nav a.active { background: rgba(0,0,0,0.1); }
|
||||
.logo { font-size: 1.4rem; font-weight: bold; }
|
||||
</style>
|
||||
</head>
|
||||
@@ -885,6 +885,67 @@ class HomeController extends Controller {
|
||||
}
|
||||
PHP;
|
||||
|
||||
$files['WEBROOT/src/MVC/Controller/AuthController.php'] = <<<'PHP'
|
||||
<?php
|
||||
|
||||
namespace MVC\Controller;
|
||||
|
||||
use MVC\Core\Controller;
|
||||
|
||||
class AuthController extends Controller {
|
||||
public function login() {
|
||||
if (isset($_SESSION['user_logged_in'])) {
|
||||
header('Location: ?service=home');
|
||||
exit;
|
||||
}
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
$username = $_POST['username'] ?? '';
|
||||
$password = $_POST['password'] ?? '';
|
||||
|
||||
// Hardcoded logins
|
||||
$users = [
|
||||
'admin' => 'password123',
|
||||
'user' => 'user123'
|
||||
];
|
||||
|
||||
if (isset($users[$username]) && $users[$username] === $password) {
|
||||
$_SESSION['user_logged_in'] = true;
|
||||
$_SESSION['username'] = $username;
|
||||
header('Location: ?service=home');
|
||||
exit;
|
||||
} else {
|
||||
return $this->render('login', ['error' => 'Ungültige Anmeldedaten']);
|
||||
}
|
||||
}
|
||||
|
||||
return $this->render('login');
|
||||
}
|
||||
|
||||
public function logout() {
|
||||
session_destroy();
|
||||
header('Location: ?service=login');
|
||||
exit;
|
||||
}
|
||||
}
|
||||
PHP;
|
||||
|
||||
$files['WEBROOT/src/MVC/Controller/DocsController.php'] = <<<'PHP'
|
||||
<?php
|
||||
|
||||
namespace MVC\Controller;
|
||||
|
||||
use MVC\Core\Controller;
|
||||
|
||||
class DocsController extends Controller {
|
||||
public function index() {
|
||||
return $this->render('docs', [
|
||||
'title' => '📚 System-Dokumentation'
|
||||
]);
|
||||
}
|
||||
}
|
||||
PHP;
|
||||
|
||||
$files['WEBROOT/src/MVC/Controller/TestController.php'] = <<<'PHP'
|
||||
<?php
|
||||
namespace MVC\Controller;
|
||||
@@ -943,6 +1004,128 @@ $files['WEBROOT/src/MVC/View/home.php'] = "<h1>Dashboard</h1><p>Willkommen in de
|
||||
$files['WEBROOT/src/MVC/View/test1.php'] = "<h1>Service 1</h1><p><?= \$msg ?></p>";
|
||||
$files['WEBROOT/src/MVC/View/test2.php'] = "<h1>System Check</h1><ul><?php foreach(\$data as \$k => \$v): ?><li><b><?= \$k ?>:</b> <?= \$v ?></li><?php endforeach; ?></ul>";
|
||||
|
||||
$files['WEBROOT/src/MVC/View/docs.php'] = <<<'HTML'
|
||||
<div class="docs-container">
|
||||
<h1>🏛️ Framework Architektur & Dokumentation</h1>
|
||||
|
||||
<div class="toc">
|
||||
<h3>Inhalt</h3>
|
||||
<ul>
|
||||
<li><a href="#intro">1. Einführung</a></li>
|
||||
<li><a href="#files">2. Datei-Referenz (Deep Dive)</a></li>
|
||||
<li><a href="#api">3. API-System</a></li>
|
||||
<li><a href="#database">4. Datenbank & Models</a></li>
|
||||
<li><a href="#classes">5. Helper Klassen</a></li>
|
||||
<li><a href="#generators">6. CLI-Generatoren</a></li>
|
||||
<li><a href="#best-practices">7. Best Practices</a></li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<section id="intro">
|
||||
<h2>1. Einführung</h2>
|
||||
<p>Dieses Framework basiert auf dem <strong>Model-View-Controller (MVC)</strong> Entwurfsmuster und nutzt einen <strong>Front Controller</strong> (index.php). Alle Anfragen werden zentral verarbeitet, was globale Middleware (wie Authentifizierung) und ein einheitliches Layout ermöglicht.</p>
|
||||
</section>
|
||||
|
||||
<section id="files">
|
||||
<h2>2. Datei-Referenz (Deep Dive)</h2>
|
||||
<h3>Framework Core</h3>
|
||||
<ul>
|
||||
<li><strong>Router.php:</strong> Verwaltet die Routen. Unterstützt Middlewares. Das Dispatching erfolgt über den <code>service</code> Parameter in der URL.</li>
|
||||
<li><strong>Controller.php:</strong> Basisklasse für alle Controller. Bietet <code>render()</code> für Views und <code>json()</code> für API-Responses.</li>
|
||||
<li><strong>Model.php:</strong> Abstrakte Basis für Datenbank-Entitäten. Kapselt die <code>databaseMethods.php</code> in ein objektorientiertes Interface.</li>
|
||||
<li><strong>View.php:</strong> Behandelt das Extrahieren von Daten in HTML-Templates.</li>
|
||||
</ul>
|
||||
<h3>Konfiguration</h3>
|
||||
<ul>
|
||||
<li><strong>env.ini:</strong> Speichert Datenbank-Zugangsdaten für <code>dev</code> und <code>live</code> Umgebungen.</li>
|
||||
<li><strong>autoloader.php:</strong> Registriert einen PSR-4 ähnlichen Autoloader für den <code>MVC\</code> Namespace.</li>
|
||||
</ul>
|
||||
</section>
|
||||
|
||||
<section id="api">
|
||||
<h2>3. API-System</h2>
|
||||
<p>APIs liegen im Verzeichnis <code>WEBROOT/api/v1/</code>. Sie nutzen die <code>ApiController</code>-Klasse.</p>
|
||||
<pre>
|
||||
// Beispiel: WEBROOT/api/v1/get.php
|
||||
use MVC\Classes\Api;
|
||||
use MVC\Controller\ApiController;
|
||||
|
||||
Api::jsonResponse((new ApiController())->get());
|
||||
</pre>
|
||||
</section>
|
||||
|
||||
<section id="database">
|
||||
<h2>4. Datenbank & Models</h2>
|
||||
<p>Das Framework nutzt <code>mysqli</code> mit Prepared Statements für maximale Sicherheit.</p>
|
||||
<h3>databaseMethods.php (Die Power-Tools)</h3>
|
||||
<ul>
|
||||
<li><code>DatabaseShow($table, $columns, $where, ...$values)</code></li>
|
||||
<li><code>DatabaseInsert($table, $columns, ...$values)</code></li>
|
||||
<li><code>DatabaseUpdate($table, $set, $where, ...$values)</code></li>
|
||||
<li><code>DatabaseDelete($table, $where, ...$values)</code></li>
|
||||
</ul>
|
||||
</section>
|
||||
|
||||
<section id="classes">
|
||||
<h2>5. Helper Klassen (MVC\Classes)</h2>
|
||||
<ul>
|
||||
<li><strong>Request:</strong> Zugriff auf <code>method()</code>, <code>queryParams()</code>, <code>headers()</code>.</li>
|
||||
<li><strong>Client:</strong> Ermittelt <code>ip()</code>, <code>userAgent()</code>, <code>country()</code>.</li>
|
||||
<li><strong>Cookie:</strong> Interface zum <code>set()</code>, <code>read()</code>, <code>Delete()</code> von Cookies.</li>
|
||||
</ul>
|
||||
</section>
|
||||
|
||||
<section id="generators">
|
||||
<h2>6. CLI-Generatoren</h2>
|
||||
<p><code>php create_module.php [Name]</code> - Erstellt Controller & View.</p>
|
||||
<p><code>php create_model.php [Tabelle] [Spalten]</code> - Erstellt ein Model.</p>
|
||||
</section>
|
||||
|
||||
<section id="best-practices">
|
||||
<h2>7. Best Practices</h2>
|
||||
<p><strong>Naming:</strong> Controller immer mit <code>Controller</code>-Suffix. Models im Singular.</p>
|
||||
<p><strong>Security:</strong> Nutze <code>['auth']</code> Middleware konsequent für alle internen Seiten.</p>
|
||||
</section>
|
||||
</div>
|
||||
|
||||
<style>
|
||||
.docs-container { max-width: 1000px; color: var(--text); }
|
||||
.toc { background: var(--sidebar); padding: 20px; border-radius: 8px; margin-bottom: 30px; border: 1px solid var(--border); }
|
||||
.toc h3 { margin-top: 0; }
|
||||
section { margin-bottom: 50px; padding-bottom: 20px; border-bottom: 1px solid var(--border); }
|
||||
h2 { color: var(--accent); font-size: 1.8rem; margin-bottom: 20px; }
|
||||
pre { background: #1e1e1e; color: #d4d4d4; padding: 15px; border-radius: 5px; overflow-x: auto; border: 1px solid #333; font-size: 0.9rem; }
|
||||
</style>
|
||||
HTML;
|
||||
|
||||
$files['WEBROOT/src/MVC/View/login.php'] = <<<'HTML'
|
||||
<style>
|
||||
.login-container { max-width: 400px; margin: 50px auto; padding: 30px; border: 1px solid var(--border); border-radius: 8px; background: var(--sidebar); box-shadow: 0 4px 15px rgba(0,0,0,0.1); }
|
||||
.login-container h2 { text-align: center; margin-top: 0; color: var(--text); }
|
||||
.login-container input { width: 100%; padding: 12px; margin: 10px 0; border: 1px solid var(--border); border-radius: 4px; box-sizing: border-box; background: var(--bg); color: var(--text); }
|
||||
.login-container button { width: 100%; padding: 12px; background: var(--header); color: white; border: none; border-radius: 4px; cursor: pointer; font-size: 1rem; font-weight: bold; }
|
||||
.login-container button:hover { opacity: 0.9; }
|
||||
.error-msg { color: #e74c3c; background: rgba(231, 76, 60, 0.1); padding: 10px; border-radius: 4px; margin-bottom: 15px; text-align: center; font-size: 0.9rem; }
|
||||
</style>
|
||||
<div class="login-container">
|
||||
<h2>🔑 Login</h2>
|
||||
<?php if (isset($error)): ?>
|
||||
<div class="error-msg"><?= htmlspecialchars($error) ?></div>
|
||||
<?php endif; ?>
|
||||
<form method="POST">
|
||||
<div>
|
||||
<label>Benutzername</label>
|
||||
<input type="text" name="username" required autofocus placeholder="Admin / User">
|
||||
</div>
|
||||
<div>
|
||||
<label>Passwort</label>
|
||||
<input type="password" name="password" required placeholder="********">
|
||||
</div>
|
||||
<button type="submit">Anmelden</button>
|
||||
</form>
|
||||
</div>
|
||||
HTML;
|
||||
|
||||
// --- ENTRY POINT ---
|
||||
$files['WEBROOT/index.php'] = <<<'PHP'
|
||||
<?php
|
||||
@@ -951,36 +1134,62 @@ require_once __DIR__ . '/src/MVC/autoloader.php';
|
||||
$service = $_GET['service'] ?? 'home';
|
||||
try {
|
||||
$router = new \MVC\Core\Router();
|
||||
|
||||
// LOGIN EIN/AUSSCHALTEN: Einfach ['auth'] als 4. Parameter hinzufügen oder entfernen
|
||||
$router->add('home', \MVC\Controller\HomeController::class, 'index');
|
||||
$router->add('toggle_theme', \MVC\Controller\HomeController::class, 'toggle_theme');
|
||||
$router->add('test1', \MVC\Controller\TestController::class, 'service1');
|
||||
$router->add('test2', \MVC\Controller\TestController::class, 'service2');
|
||||
$router->add('docs', \MVC\Controller\DocsController::class, 'index');
|
||||
$router->add('test1', \MVC\Controller\TestController::class, 'service1', ['auth']);
|
||||
$router->add('test2', \MVC\Controller\TestController::class, 'service2', ['auth']);
|
||||
$router->add('login', \MVC\Controller\AuthController::class, 'login');
|
||||
$router->add('logout', \MVC\Controller\AuthController::class, 'logout');
|
||||
|
||||
$layout = new \MVC\Layout\Layout('main');
|
||||
$layout->setBlock('title', 'MVC Starter');
|
||||
$layout->setBlock('header_title', '🚀 MVC Dashboard');
|
||||
$layout->setBlock('header_actions', '<button class="btn-toggle" onclick="toggleTheme()">🌓 Theme umschalten</button>');
|
||||
|
||||
$headerActions = '<button class="btn-toggle" onclick="toggleTheme()">🌓 Theme umschalten</button>';
|
||||
if (isset($_SESSION['user_logged_in'])) {
|
||||
$headerActions = '<span style="margin-right: 15px; font-size: 0.9rem;">👤 ' . htmlspecialchars($_SESSION['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();
|
||||
$navItems = [
|
||||
'home' => '🏠 Home',
|
||||
'test1' => '🛠️Test 1',
|
||||
'test2' => '📊 Test 2',
|
||||
'home' => ['label' => '🏠 Home', 'auth' => false],
|
||||
'test1' => ['label' => '🛠️ Test 1', 'auth' => true],
|
||||
'test2' => ['label' => '📊 Test 2', 'auth' => true],
|
||||
'docs' => ['label' => '📚 Dokumentation', 'auth' => false],
|
||||
];
|
||||
?>
|
||||
<nav>
|
||||
<?php foreach ($navItems as $navKey => $navLabel): ?>
|
||||
<a href="?service=<?= $navKey ?>" class="<?= $service === $navKey ? 'active' : '' ?>"><?= $navLabel ?></a>
|
||||
<?php foreach ($navItems as $navKey => $item):
|
||||
if ($item['auth'] && !isset($_SESSION['user_logged_in'])) continue;
|
||||
?>
|
||||
<a href="?service=<?= $navKey ?>" class="<?= $service === $navKey ? 'active' : '' ?>"><?= $item['label'] ?></a>
|
||||
<?php endforeach; ?>
|
||||
|
||||
<?php if (!isset($_SESSION['user_logged_in'])): ?>
|
||||
<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', $router->dispatch($service));
|
||||
echo $layout->render();
|
||||
} catch (\Exception $e) {
|
||||
http_response_code(404); echo "<h1>404</h1><p>" . $e->getMessage() . "</p>";
|
||||
} catch (\Throwable $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>";
|
||||
}
|
||||
PHP;
|
||||
|
||||
@@ -1159,7 +1368,8 @@ if (!file_exists($viewFile)) {
|
||||
// 3. Anleitung ausgeben
|
||||
echo "\n--- NÄCHSTE SCHRITTE ---\n";
|
||||
echo " Registriere das Modul in WEBROOT/index.php beim Router:\n";
|
||||
echo " \$router->add('{$key}', \\MVC\\Controller\\{$className}Controller::class, '{$key}', ['auth']);\n\n";
|
||||
echo " \$router->add('{$key}', \\MVC\\Controller\\{$className}Controller::class, '{$key}', ['auth']);\n";
|
||||
echo " (Lösche ['auth'] wenn kein Login erforderlich ist)\n\n";
|
||||
echo "------------------------\n";
|
||||
PHP;
|
||||
|
||||
@@ -1173,4 +1383,4 @@ foreach ($files as $file => $content) {
|
||||
echo "\n✨ Projekt '$projectName' wurde initialisiert!\n";
|
||||
echo "1. Richte deine Datenbankverbindung in WEBROOT/src/config/env.ini ein.\n";
|
||||
echo "2. Starten: php server.php\n";
|
||||
echo "3. Öffnen: http://localhost:8000\n";
|
||||
echo "3. Öffnen: http://localhost:8000\n";
|
||||
Reference in new issue
Block a user