owowowo
This commit is contained in:
@@ -1,4 +1,2 @@
|
||||
<?php
|
||||
$router->get('/', function () {
|
||||
require_once __DIR__ . '/../templates/base.php';
|
||||
});
|
||||
$router->get('/', '/', [new HomeController(), 'home']);
|
||||
32
src/Controller/BaseController.php
Normal file
32
src/Controller/BaseController.php
Normal file
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
abstract class BaseController
|
||||
{
|
||||
protected function render(string $view, array $data = []): void
|
||||
{
|
||||
extract($data);
|
||||
|
||||
$viewPath = __DIR__ . '/../../views/' . $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;
|
||||
}
|
||||
}
|
||||
10
src/Controller/HomeController.php
Normal file
10
src/Controller/HomeController.php
Normal file
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
class HomeController extends BaseController
|
||||
{
|
||||
public function index(): void
|
||||
{
|
||||
$this->render('home', [
|
||||
'title' => 'Willkommen'
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
<?php
|
||||
$title = 'Home';
|
||||
|
||||
ob_start();
|
||||
?>
|
||||
<h1>Welcome</h1>
|
||||
<p>This is the homepage.</p>
|
||||
<?php
|
||||
$content = ob_get_clean();
|
||||
require_once __DIR__ . '/layout.php';
|
||||
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';
|
||||
@@ -1,9 +1,8 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<html lang="de">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title><?= $title ?? 'Acme' ?></title>
|
||||
<title><?= htmlspecialchars($title) ?></title>
|
||||
</head>
|
||||
<body>
|
||||
<?= $content ?>
|
||||
|
||||
Reference in New Issue
Block a user