refactor: Umstellung auf Mini-MVC-Architektur (Issue #46)
All checks were successful
Deploy Feature Branch to Test / deploy (push) Successful in 24s
All checks were successful
Deploy Feature Branch to Test / deploy (push) Successful in 24s
- Front Controller Pattern mit public/index.php als Einstiegspunkt - Eigenes Routing (App\Core\Router) ohne externes Framework - Controller: HomeController, ImpressumController, DatenschutzController - Views mit gemeinsamem Layout (app/views/layouts/main.php) - PSR-4 Autoloading - Statische Assets nach public/ verschoben - Alte Dateien (index.php, impressum.html, datenschutz.html) geloescht - 301-Redirects fuer alte URLs - PHP 8.5 kompatibel - Apache DocumentRoot auf public/ gesetzt
This commit is contained in:
46
app/core/View.php
Normal file
46
app/core/View.php
Normal file
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Core;
|
||||
|
||||
class View
|
||||
{
|
||||
private string $viewsPath;
|
||||
private array $data = [];
|
||||
|
||||
public function __construct(?string $viewsPath = null)
|
||||
{
|
||||
$this->viewsPath = $viewsPath ?? dirname(__DIR__) . '/views';
|
||||
}
|
||||
|
||||
public function assign(string $key, mixed $value): void
|
||||
{
|
||||
$this->data[$key] = $value;
|
||||
}
|
||||
|
||||
public function render(string $view, string $layout = 'main'): void
|
||||
{
|
||||
$viewFile = $this->viewsPath . '/' . $view . '.php';
|
||||
$layoutFile = $this->viewsPath . '/layouts/' . $layout . '.php';
|
||||
|
||||
if (!file_exists($viewFile)) {
|
||||
throw new \RuntimeException("View {$view} nicht gefunden: {$viewFile}");
|
||||
}
|
||||
|
||||
if (!file_exists($layoutFile)) {
|
||||
throw new \RuntimeException("Layout {$layout} nicht gefunden: {$layoutFile}");
|
||||
}
|
||||
|
||||
// Extract data to variables for the view
|
||||
extract($this->data, EXTR_SKIP);
|
||||
|
||||
// Capture view content
|
||||
ob_start();
|
||||
require $viewFile;
|
||||
$content = ob_get_clean();
|
||||
|
||||
// Render layout with $content
|
||||
require $layoutFile;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user