Docs: Handler-Typen und Response-Methoden mit Beispielen in README ergänzt
This commit is contained in:
parent
5157f5b86e
commit
801ce59233
1 changed files with 164 additions and 0 deletions
164
README.md
164
README.md
|
|
@ -104,9 +104,13 @@ $router->group('/api', function ($router) {
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
|
||||||
## Response-Helper
|
## Response-Helper
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
```php
|
```php
|
||||||
|
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
return (new Response())->withJson(['ok' => true]);
|
return (new Response())->withJson(['ok' => true]);
|
||||||
|
|
@ -116,6 +120,94 @@ return (new Response())->withRedirect('/login');
|
||||||
return (new Response())->withDownload('datei.txt', 'Inhalt...');
|
return (new Response())->withDownload('datei.txt', 'Inhalt...');
|
||||||
```
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Handler-Typen
|
||||||
|
|
||||||
|
Dein Router unterstützt verschiedene Arten von Handlern für Routen:
|
||||||
|
|
||||||
|
| Handler-Typ | Beispiel | Beschreibung |
|
||||||
|
|-----------------------------------|-----------------------------------------------|---------------------------------------------------|
|
||||||
|
| **Closure (anonyme Funktion)** | `function (Request $req, array $params) { ... }` | Direkt im Routing-Code definierte Funktion |
|
||||||
|
| **Controller-String** | `'App\\Controller\\UserController@index'` | String im Format `Controller@Methode` |
|
||||||
|
| **Controller-Array (klassisch)** | `[UserController::class, 'index']` | Array mit Klassenname und Methodenname |
|
||||||
|
| **Controller-Array (Instanz)** | `[$controllerObj, 'index']` | Array mit Controller-Objekt und Methodenname |
|
||||||
|
|
||||||
|
### Beispiele
|
||||||
|
|
||||||
|
```php
|
||||||
|
<?php
|
||||||
|
|
||||||
|
// Closure
|
||||||
|
$router->get('/hello', function (Request $request, array $params) {
|
||||||
|
return (new Response())->withText('Hallo Welt!');
|
||||||
|
});
|
||||||
|
|
||||||
|
// Controller-String
|
||||||
|
$router->get('/api/users', 'App\\Controller\\UserController@index');
|
||||||
|
|
||||||
|
// Controller-Array (klassisch, empfohlen)
|
||||||
|
$router->get('/api/users', [UserController::class, 'index']);
|
||||||
|
|
||||||
|
// Controller-Array (mit Instanz, selten nötig)
|
||||||
|
$userController = new UserController();
|
||||||
|
$router->get('/api/users', [$userController, 'index']);
|
||||||
|
```
|
||||||
|
|
||||||
|
Jeder Handler erhält immer:
|
||||||
|
|
||||||
|
```php
|
||||||
|
<?php
|
||||||
|
|
||||||
|
function (Request $request, array $params)
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Response-Methoden
|
||||||
|
|
||||||
|
Die Response-Klasse bietet folgende Methoden:
|
||||||
|
|
||||||
|
| Methode | Beschreibung | Beispiel |
|
||||||
|
|--------------------------|-----------------------------------------------------------------------------------------------|-----------------------------------------------|
|
||||||
|
| `withJson($data, $statusCode = 200)` | Antwort als JSON, setzt Content-Type automatisch | `$response->withJson(['ok' => true])` |
|
||||||
|
| `withHtml($html, $statusCode = 200)` | Antwort als HTML, setzt Content-Type automatisch | `$response->withHtml('<h1>Hello</h1>')` |
|
||||||
|
| `withText($text, $statusCode = 200)` | Antwort als Plaintext, setzt Content-Type automatisch | `$response->withText('Nur Text')` |
|
||||||
|
| `withRedirect($location, $statusCode = 302)` | HTTP-Redirect, setzt Location-Header und Statuscode | `$response->withRedirect('/login')` |
|
||||||
|
| `withDownload($filename, $content, $contentType = 'application/octet-stream')` | Antwort als Datei-Download, setzt passende Header | `$response->withDownload('file.txt', 'Inhalt')`|
|
||||||
|
| `withStatus($code)` | Antwort mit anderem HTTP-Statuscode (z.B. 404, 500) | `$response->withStatus(404)` |
|
||||||
|
| `withHeader($name, $value)`| Setzt einen beliebigen HTTP-Header | `$response->withHeader('X-Test', 'Wert')` |
|
||||||
|
| `withBody($body)` | Antwort mit anderem Body | `$response->withBody('Neuer Body')` |
|
||||||
|
| `send()` | Sendet die Antwort an den Client (wird meist automatisch am Ende aufgerufen) | `$response->send()` |
|
||||||
|
|
||||||
|
### Beispiele
|
||||||
|
|
||||||
|
```php
|
||||||
|
<?php
|
||||||
|
|
||||||
|
// JSON-Antwort
|
||||||
|
return (new Response())->withJson(['success' => true]);
|
||||||
|
|
||||||
|
// HTML-Antwort
|
||||||
|
return (new Response())->withHtml('<h1>Hallo Welt!</h1>');
|
||||||
|
|
||||||
|
// Plaintext
|
||||||
|
return (new Response())->withText('Nur Text');
|
||||||
|
|
||||||
|
// Redirect
|
||||||
|
return (new Response())->withRedirect('/login');
|
||||||
|
|
||||||
|
// Datei-Download
|
||||||
|
return (new Response())->withDownload('beispiel.txt', 'Dateiinhalt', 'text/plain');
|
||||||
|
|
||||||
|
// Statuscode ändern
|
||||||
|
return (new Response())->withStatus(404)->withText('Nicht gefunden');
|
||||||
|
|
||||||
|
// Header setzen
|
||||||
|
return (new Response())->withHeader('X-Custom', 'Wert')->withJson(['ok' => true]);
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## Routen-Dokumentation
|
## Routen-Dokumentation
|
||||||
|
|
@ -149,16 +241,88 @@ bin/ # CLI-Tools
|
||||||
cache/ # Routen-Cache (JSON)
|
cache/ # Routen-Cache (JSON)
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
## Dependency Injection & Mini-Container
|
||||||
|
|
||||||
|
Du kannst dein Projekt mit einem einfachen Dependency Injection Container (DI-Container) noch flexibler machen. Damit kannst du Controller, Services oder Middleware automatisch instanziieren und Abhängigkeiten verwalten.
|
||||||
|
|
||||||
|
### Beispiel: Mini-Container
|
||||||
|
|
||||||
|
```php
|
||||||
|
<?php
|
||||||
|
|
||||||
|
class Container
|
||||||
|
{
|
||||||
|
protected array $bindings = [];
|
||||||
|
protected array $instances = [];
|
||||||
|
|
||||||
|
// Registrierung einer Factory oder eines Singleton
|
||||||
|
public function set(string $id, callable $factory, bool $singleton = false)
|
||||||
|
{
|
||||||
|
$this->bindings[$id] = ['factory' => $factory, 'singleton' => $singleton];
|
||||||
|
}
|
||||||
|
|
||||||
|
// Hole eine Instanz (Singleton oder Factory)
|
||||||
|
public function get(string $id)
|
||||||
|
{
|
||||||
|
// Singleton bereits erzeugt?
|
||||||
|
if (isset($this->instances[$id])) {
|
||||||
|
return $this->instances[$id];
|
||||||
|
}
|
||||||
|
|
||||||
|
// Factory vorhanden?
|
||||||
|
if (isset($this->bindings[$id])) {
|
||||||
|
$object = ($this->bindings[$id]['factory'])($this);
|
||||||
|
if ($this->bindings[$id]['singleton']) {
|
||||||
|
$this->instances[$id] = $object;
|
||||||
|
}
|
||||||
|
return $object;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fallback: Standard-Konstruktor (ohne Abhängigkeiten)
|
||||||
|
return new $id();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
**Verwendung:**
|
||||||
|
|
||||||
|
```php
|
||||||
|
<?php
|
||||||
|
|
||||||
|
$container = new Container();
|
||||||
|
|
||||||
|
// Singleton-Registrierung
|
||||||
|
$container->set(MyService::class, fn($c) => new MyService(), true);
|
||||||
|
|
||||||
|
// Factory (immer neue Instanz)
|
||||||
|
$container->set(MyOtherService::class, fn($c) => new MyOtherService($c->get(MyService::class)));
|
||||||
|
|
||||||
|
// Controller automatisch mit Container erzeugen
|
||||||
|
$controller = $container->get(UserController::class);
|
||||||
|
```
|
||||||
|
|
||||||
|
**Tipp:** Du kannst den Container im Router hinterlegen und beim Dispatch für Controller-Handler nutzen.
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## Weiterführendes
|
## Weiterführendes
|
||||||
|
|
||||||
- **Eigene Controller:** Lege sie unter `example/app/Controller` ab und verwende sie als Handler.
|
- **Eigene Controller:** Lege sie unter `example/app/Controller` ab und verwende sie als Handler.
|
||||||
|
|
||||||
- **Middleware:** Schreibe eigene Middleware als Closure oder Klasse.
|
- **Middleware:** Schreibe eigene Middleware als Closure oder Klasse.
|
||||||
|
|
||||||
- **Tests & Erweiterungen:** Das Framework ist modular und kann leicht erweitert werden.
|
- **Tests & Erweiterungen:** Das Framework ist modular und kann leicht erweitert werden.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
|
||||||
## Lizenz
|
## Lizenz
|
||||||
|
|
||||||
MIT
|
MIT
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue