Zum Hauptinhalt springen

UI-Schicht

Eingangsschicht der Anwendung: hier beginnt der Datenfluss.

Die Rolle der UI-Schicht

Die UI-Schicht bildet das Interface zur Außenwelt: Hier kommen HTTP-Requests an, Konsolenbefehle werden empfangen oder Formulare verarbeitet. Ihr Ziel ist es, Daten entgegenzunehmen, diese an die Anwendungs-Schicht weiterzugeben und die Antworten wieder darzustellen – in HTML, JSON, Text oder was auch immer dein Kanal verlangt.

Die UI-Schicht kennt keine Geschäftslogik, sondern dient der Koordination zwischen Anwender:innen und Anwendungsfällen.

Typische Bestandteile der UI-Schicht

src/
└── UI/
├── Http/
│ ├── Controller/ # Symfony-Controller für HTTP-Routen
│ └── Request/ # Request-Objekte (z. B. Form- oder JSON-Daten)
├── Console/ # Symfony-Commands
├── Form/ # Symfony-Formulare
└── Web/ # Templates (z. B. Twig)

HTTP-Controller

Controller sind typische Einstiegspunkte über HTTP und delegieren an Use Cases in der Anwendungs-Schicht.

// src/UI/Http/Controller/ProductController.php

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use App\Application\Command\CreateProduct;

class ProductController extends AbstractController
{
#[Route('/products', methods: ['POST'])]
public function create(Request $request, MessageBusInterface $bus): Response
{
$data = json_decode($request->getContent(), true);

$command = new CreateProduct(
$data['id'],
$data['name'],
$data['price']
);

$bus->dispatch($command);

return new Response('Produkt erstellt', 201);
}
}

Console-Commands

// src/UI/Console/CreateProductCommand.php

use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

#[AsCommand(name: 'app:create-product')]
class CreateProductCommand extends Command
{
protected function execute(InputInterface $input, OutputInterface $output): int
{
// Command an Application-Schicht weiterleiten
return Command::SUCCESS;
}
}

Forms und Views

Die UI-Schicht kann auch Formulare kapseln oder View-spezifische Logik enthalten, z. B. Templates mit Twig:

{# templates/product/show.html.twig #}
<h1>{{ product.name }}</h1>
<p>Preis: {{ product.priceInCents / 100 }} &euro;</p>

Zusammenfassung

Die UI-Schicht:

  • nimmt Eingaben entgegen (HTTP, Console, Formulare)
  • reicht Daten an Use Cases (Application) weiter
  • rendert Ausgaben oder liefert strukturierte Daten zurück
  • enthält keine Geschäftslogik, sondern nur Formatierung, Weiterleitung und Darstellung

Sie bildet die äußere Hülle deiner Anwendung und spricht mit Benutzer:innen und Maschinen gleichermaßen.