Backup #2094

ID2094
Dateipfad/var/www/dev.campus.systemische-tools.de/src/Framework/Http/ViewTrait.php
Version1
Typ modified
Größe1.9 KB
Hash359b4bdb0c57c572ba5bac730441ae85251e5249ee5454902f02447e39408a35
Datum2025-12-29 00:19:03
Geändert vonclaude-code-hook
GrundClaude Code Pre-Hook Backup vor Edit-Operation
Datei existiert Ja

Dateiinhalt

<?php

declare(strict_types=1);

namespace Framework\Controller;

// @responsibility: View-Rendering für Controller

trait ViewTrait
{
    /**
     * Render a full view template.
     *
     * @param array<string, mixed> $data
     */
    protected function view(string $name, array $data = []): void
    {
        $data['csrfField'] = $this->csrfField();
        $data['csrfToken'] = $this->csrfToken();
        $data['flashSuccess'] = $this->consumeFlash('success');
        $data['flashError'] = $this->consumeFlash('error');
        $__viewPath = VIEW_PATH . '/' . str_replace('.', '/', $name) . '.php';
        extract($data);

        if (file_exists($__viewPath)) {
            require $__viewPath;
        } else {
            throw new \Exception("View not found: {$name}");
        }
    }

    /**
     * Render a partial template (for HTMX responses).
     *
     * @param array<string, mixed> $data
     */
    protected function partial(string $name, array $data = []): void
    {
        $data['csrfField'] = $this->csrfField();
        $data['csrfToken'] = $this->csrfToken();
        extract($data);

        $paths = [
            VIEW_PATH . '/' . str_replace('.', '/', $name) . '.php',
            VIEW_PATH . '/partials/' . str_replace('.', '/', $name) . '.php',
        ];

        foreach ($paths as $file) {
            if (file_exists($file)) {
                require $file;

                return;
            }
        }

        throw new \Exception("Partial not found: {$name}");
    }

    /**
     * Consume flash message from session.
     */
    protected function consumeFlash(string $key): ?string
    {
        $value = $_SESSION[$key] ?? null;
        if ($value !== null) {
            unset($_SESSION[$key]);
        }

        return $value;
    }

    /**
     * Set flash message in session.
     */
    protected function flash(string $key, string $message): void
    {
        $_SESSION[$key] = $message;
    }
}

Vollständig herunterladen

Aktionen

Herunterladen

← Zurück zur Übersicht