API/Router: Routen und Insomnia-Collection synchronisiert, PATCH/OPTIONS/GET für /api/users/{id} ergänzt, Doku aktualisiert
This commit is contained in:
parent
0c21bceb82
commit
243101bf1f
5 changed files with 209 additions and 74 deletions
|
|
@ -118,6 +118,7 @@
|
|||
"_id": "req_api_users",
|
||||
"parentId": "fld_api",
|
||||
"name": "API: Alle Users (GET)",
|
||||
"description": "Gibt eine Liste aller User als JSON zurück.\n\n**Antwort:**\n```\n{\n \"users\": [\"Alice\", \"Bob\", \"Charlie\"]\n}\n```",
|
||||
"method": "GET",
|
||||
"url": "{{ base_url }}/api/users",
|
||||
"body": {},
|
||||
|
|
@ -125,10 +126,43 @@
|
|||
"headers": [],
|
||||
"_type": "request"
|
||||
},
|
||||
{
|
||||
"_id": "req_api_users_options",
|
||||
"parentId": "fld_api",
|
||||
"name": "API: Users Optionen (OPTIONS)",
|
||||
"description": "Gibt die unterstützten Methoden für /api/users zurück (CORS/Preflight).\n\n**Antwort:**\nHeader: `Allow: GET,POST,PUT,DELETE,PATCH,OPTIONS`\nBody: `{ \"allow\": \"GET,POST,PUT,DELETE,PATCH,OPTIONS\" }`",
|
||||
"method": "OPTIONS",
|
||||
"url": "{{ base_url }}/api/users",
|
||||
"body": {},
|
||||
"parameters": [],
|
||||
"headers": [],
|
||||
"_type": "request"
|
||||
},
|
||||
{
|
||||
"_id": "req_api_users_patch",
|
||||
"parentId": "fld_api",
|
||||
"name": "API: Users Teil-Update (PATCH)",
|
||||
"description": "Teilweises Update für alle User (Demo, keine echte Funktion).",
|
||||
"method": "PATCH",
|
||||
"url": "{{ base_url }}/api/users",
|
||||
"body": {
|
||||
"mimeType": "application/json",
|
||||
"text": "{\n \"field\": \"value\"\n}"
|
||||
},
|
||||
"parameters": [],
|
||||
"headers": [
|
||||
{
|
||||
"name": "Content-Type",
|
||||
"value": "application/json"
|
||||
}
|
||||
],
|
||||
"_type": "request"
|
||||
},
|
||||
{
|
||||
"_id": "req_api_user_id",
|
||||
"parentId": "fld_api",
|
||||
"name": "API: User mit ID (GET)",
|
||||
"description": "Gibt einen User mit der angegebenen ID zurück.\n\n**Antwort:**\n```\n{\n \"user_id\": 42,\n \"profile_url\": \"/user/42\"\n}\n```",
|
||||
"method": "GET",
|
||||
"url": "{{ base_url }}/api/users/42",
|
||||
"body": {},
|
||||
|
|
@ -136,10 +170,43 @@
|
|||
"headers": [],
|
||||
"_type": "request"
|
||||
},
|
||||
{
|
||||
"_id": "req_api_user_id_patch",
|
||||
"parentId": "fld_api",
|
||||
"name": "API: User Teil-Update (PATCH)",
|
||||
"description": "Teilweises Update eines Users mit der angegebenen ID.\n\n**Antwort:**\n```\n{\n \"message\": \"User partially updated\",\n \"id\": 42\n}\n```",
|
||||
"method": "PATCH",
|
||||
"url": "{{ base_url }}/api/users/42",
|
||||
"body": {
|
||||
"mimeType": "application/json",
|
||||
"text": "{\n \"name\": \"Teilupdate\"\n}"
|
||||
},
|
||||
"parameters": [],
|
||||
"headers": [
|
||||
{
|
||||
"name": "Content-Type",
|
||||
"value": "application/json"
|
||||
}
|
||||
],
|
||||
"_type": "request"
|
||||
},
|
||||
{
|
||||
"_id": "req_api_user_id_options",
|
||||
"parentId": "fld_api",
|
||||
"name": "API: User Optionen (OPTIONS)",
|
||||
"description": "Gibt die unterstützten Methoden für /api/users/{id} zurück (CORS/Preflight).\n\n**Antwort:**\nHeader: `Allow: GET,POST,PUT,DELETE,PATCH,OPTIONS`\nBody: `{ \"allow\": \"GET,POST,PUT,DELETE,PATCH,OPTIONS\" }`",
|
||||
"method": "OPTIONS",
|
||||
"url": "{{ base_url }}/api/users/42",
|
||||
"body": {},
|
||||
"parameters": [],
|
||||
"headers": [],
|
||||
"_type": "request"
|
||||
},
|
||||
{
|
||||
"_id": "req_api_user_create",
|
||||
"parentId": "fld_api",
|
||||
"name": "API: User anlegen (POST)",
|
||||
"description": "Legt einen neuen User an.\n\n**Body:**\n```\n{\n \"name\": \"Insomnia User\"\n}\n```\n**Antwort:**\n```\n{\n \"message\": \"User created\"\n}\n```",
|
||||
"method": "POST",
|
||||
"url": "{{ base_url }}/api/users",
|
||||
"body": {
|
||||
|
|
@ -159,6 +226,7 @@
|
|||
"_id": "req_api_user_update",
|
||||
"parentId": "fld_api",
|
||||
"name": "API: User aktualisieren (PUT)",
|
||||
"description": "Aktualisiert einen User mit der angegebenen ID.\n\n**Body:**\n```\n{\n \"name\": \"Neuer Name\"\n}\n```\n**Antwort:**\n```\n{\n \"message\": \"User updated\",\n \"id\": 42\n}\n```",
|
||||
"method": "PUT",
|
||||
"url": "{{ base_url }}/api/users/42",
|
||||
"body": {
|
||||
|
|
@ -178,6 +246,7 @@
|
|||
"_id": "req_api_user_delete",
|
||||
"parentId": "fld_api",
|
||||
"name": "API: User löschen (DELETE)",
|
||||
"description": "Löscht einen User mit der angegebenen ID.\n\n**Antwort:**\n```\n{\n \"message\": \"User deleted\",\n \"id\": 42\n}\n```",
|
||||
"method": "DELETE",
|
||||
"url": "{{ base_url }}/api/users/42",
|
||||
"body": {},
|
||||
|
|
|
|||
|
|
@ -108,6 +108,27 @@ $router->group('/api', function ($router) {
|
|||
$router->options('/users', function (Request $request, array $params) {
|
||||
return (new Response())->withHeader('Allow', 'GET,POST,PUT,DELETE,PATCH,OPTIONS')->withJson(['allow' => 'GET,POST,PUT,DELETE,PATCH,OPTIONS']);
|
||||
});
|
||||
// Ergänzung: PATCH /api/users (Demo-Response für Teil-Update aller User)
|
||||
$router->patch('/users', function (Request $request, array $params) {
|
||||
return (new Response())->withJson([
|
||||
'message' => 'Alle User wurden (demo) teilweise aktualisiert.'
|
||||
]);
|
||||
});
|
||||
// Ergänzung: GET /api/users/{id}
|
||||
$router->get('/users/{id}', function (Request $request, array $params) use ($router) {
|
||||
$id = $params['id'] ?? null;
|
||||
$profileUrl = $router->url('user.profile', ['id' => $id]);
|
||||
return (new Response())->withJson([
|
||||
'user_id' => $id,
|
||||
'profile_url' => $profileUrl
|
||||
]);
|
||||
});
|
||||
// Ergänzung: OPTIONS /api/users/{id}
|
||||
$router->options('/users/{id}', function (Request $request, array $params) {
|
||||
return (new Response())
|
||||
->withHeader('Allow', 'GET,POST,PUT,DELETE,PATCH,OPTIONS')
|
||||
->withJson(['allow' => 'GET,POST,PUT,DELETE,PATCH,OPTIONS']);
|
||||
});
|
||||
});
|
||||
|
||||
$router->group('/admin', function ($router) {
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@
|
|||
namespace MyCodebox\MariaCore\Http;
|
||||
|
||||
/**
|
||||
* Vereinfachte PSR-7-inspirierte Request-Klasse.
|
||||
* Simplified PSR-7-inspired Request class.
|
||||
*/
|
||||
class Request
|
||||
{
|
||||
|
|
@ -23,7 +23,7 @@ class Request
|
|||
}
|
||||
|
||||
/**
|
||||
* Gibt die HTTP-Methode zurück.
|
||||
* Get the HTTP method.
|
||||
*/
|
||||
public function getMethod(): string
|
||||
{
|
||||
|
|
@ -31,7 +31,7 @@ class Request
|
|||
}
|
||||
|
||||
/**
|
||||
* Gibt die URI (Pfad) zurück.
|
||||
* Get the URI path.
|
||||
*/
|
||||
public function getUri(): string
|
||||
{
|
||||
|
|
@ -39,7 +39,7 @@ class Request
|
|||
}
|
||||
|
||||
/**
|
||||
* Gibt die Query-Parameter zurück.
|
||||
* Get query parameters.
|
||||
*/
|
||||
public function getQueryParams(): array
|
||||
{
|
||||
|
|
@ -47,7 +47,7 @@ class Request
|
|||
}
|
||||
|
||||
/**
|
||||
* Gibt die geparsten Body-Parameter zurück (POST-Daten).
|
||||
* Get parsed body parameters (POST data).
|
||||
*/
|
||||
public function getParsedBody(): array
|
||||
{
|
||||
|
|
@ -55,7 +55,7 @@ class Request
|
|||
}
|
||||
|
||||
/**
|
||||
* Gibt alle HTTP-Header als Array zurück.
|
||||
* Get all HTTP headers as an associative array.
|
||||
*/
|
||||
public function getHeaders(): array
|
||||
{
|
||||
|
|
@ -63,7 +63,7 @@ class Request
|
|||
}
|
||||
|
||||
/**
|
||||
* Gibt einen bestimmten Header zurück (oder null).
|
||||
* Get a specific header value or null if not found.
|
||||
*/
|
||||
public function getHeader(string $name): ?string
|
||||
{
|
||||
|
|
@ -77,7 +77,7 @@ class Request
|
|||
}
|
||||
|
||||
/**
|
||||
* Liest alle HTTP-Header aus $_SERVER.
|
||||
* Parse all HTTP headers from $_SERVER.
|
||||
*/
|
||||
protected function parseHeaders(): array
|
||||
{
|
||||
|
|
|
|||
|
|
@ -2,6 +2,11 @@
|
|||
|
||||
namespace MyCodebox\MariaCore\Http;
|
||||
|
||||
/**
|
||||
* Class Response
|
||||
*
|
||||
* Immutable HTTP response object with helpers for common response types.
|
||||
*/
|
||||
class Response
|
||||
{
|
||||
protected int $statusCode = 200;
|
||||
|
|
@ -9,7 +14,11 @@ class Response
|
|||
protected string $body = '';
|
||||
|
||||
/**
|
||||
* Konstruktor für Response.
|
||||
* Response constructor.
|
||||
*
|
||||
* @param string $body
|
||||
* @param int $statusCode
|
||||
* @param array $headers
|
||||
*/
|
||||
public function __construct(string $body = '', int $statusCode = 200, array $headers = [])
|
||||
{
|
||||
|
|
@ -19,7 +28,9 @@ class Response
|
|||
}
|
||||
|
||||
/**
|
||||
* Gibt den HTTP-Statuscode zurück.
|
||||
* Get the HTTP status code.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getStatusCode(): int
|
||||
{
|
||||
|
|
@ -27,7 +38,10 @@ class Response
|
|||
}
|
||||
|
||||
/**
|
||||
* Gibt eine neue Response mit geändertem Statuscode zurück.
|
||||
* Return a new response with a different status code.
|
||||
*
|
||||
* @param int $code
|
||||
* @return self
|
||||
*/
|
||||
public function withStatus(int $code): self
|
||||
{
|
||||
|
|
@ -37,7 +51,9 @@ class Response
|
|||
}
|
||||
|
||||
/**
|
||||
* Gibt alle Header als Array zurück.
|
||||
* Get all headers as an associative array.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getHeaders(): array
|
||||
{
|
||||
|
|
@ -45,7 +61,11 @@ class Response
|
|||
}
|
||||
|
||||
/**
|
||||
* Gibt eine neue Response mit zusätzlichem Header zurück.
|
||||
* Return a new response with an additional header.
|
||||
*
|
||||
* @param string $name
|
||||
* @param string $value
|
||||
* @return self
|
||||
*/
|
||||
public function withHeader(string $name, string $value): self
|
||||
{
|
||||
|
|
@ -55,7 +75,9 @@ class Response
|
|||
}
|
||||
|
||||
/**
|
||||
* Gibt den Body zurück.
|
||||
* Get the response body.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getBody(): string
|
||||
{
|
||||
|
|
@ -63,7 +85,10 @@ class Response
|
|||
}
|
||||
|
||||
/**
|
||||
* Gibt eine neue Response mit geändertem Body zurück.
|
||||
* Return a new response with a different body.
|
||||
*
|
||||
* @param string $body
|
||||
* @return self
|
||||
*/
|
||||
public function withBody(string $body): self
|
||||
{
|
||||
|
|
@ -73,7 +98,9 @@ class Response
|
|||
}
|
||||
|
||||
/**
|
||||
* Sendet die Response an den Client.
|
||||
* Send the response to the client.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function send(): void
|
||||
{
|
||||
|
|
@ -85,7 +112,11 @@ class Response
|
|||
}
|
||||
|
||||
/**
|
||||
* Gibt eine neue Response mit Redirect-Header zurück.
|
||||
* Return a new response with a redirect header.
|
||||
*
|
||||
* @param string $location
|
||||
* @param int $statusCode
|
||||
* @return self
|
||||
*/
|
||||
public function withRedirect(string $location, int $statusCode = 302): self
|
||||
{
|
||||
|
|
@ -97,7 +128,11 @@ class Response
|
|||
}
|
||||
|
||||
/**
|
||||
* Gibt eine neue Response mit HTML-Content-Type zurück.
|
||||
* Return a new response with HTML content.
|
||||
*
|
||||
* @param string $html
|
||||
* @param int $statusCode
|
||||
* @return self
|
||||
*/
|
||||
public function withHtml(string $html, int $statusCode = 200): self
|
||||
{
|
||||
|
|
@ -109,7 +144,11 @@ class Response
|
|||
}
|
||||
|
||||
/**
|
||||
* Gibt eine neue Response mit Text-Content-Type zurück.
|
||||
* Return a new response with plain text content.
|
||||
*
|
||||
* @param string $text
|
||||
* @param int $statusCode
|
||||
* @return self
|
||||
*/
|
||||
public function withText(string $text, int $statusCode = 200): self
|
||||
{
|
||||
|
|
@ -121,7 +160,12 @@ class Response
|
|||
}
|
||||
|
||||
/**
|
||||
* Gibt eine neue Response für Datei-Download zurück.
|
||||
* Return a new response for file download.
|
||||
*
|
||||
* @param string $filename
|
||||
* @param string $content
|
||||
* @param string $contentType
|
||||
* @return self
|
||||
*/
|
||||
public function withDownload(string $filename, string $content, string $contentType = 'application/octet-stream'): self
|
||||
{
|
||||
|
|
@ -133,9 +177,14 @@ class Response
|
|||
}
|
||||
|
||||
/**
|
||||
* Gibt eine neue Response mit JSON-Body und passendem Content-Type zurück.
|
||||
* Return a new response with JSON body and appropriate content type.
|
||||
*
|
||||
* @param mixed $data
|
||||
* @param int $statusCode
|
||||
* @param int $jsonOptions
|
||||
* @return self
|
||||
*/
|
||||
public function withJson($data, int $statusCode = 200, int $jsonOptions = 0): self
|
||||
public function withJson(mixed $data, int $statusCode = 200, int $jsonOptions = 0): self
|
||||
{
|
||||
$body = json_encode($data, $jsonOptions | JSON_UNESCAPED_UNICODE);
|
||||
$clone = clone $this;
|
||||
|
|
|
|||
100
src/Router.php
100
src/Router.php
|
|
@ -5,52 +5,54 @@ namespace MyCodebox\MariaCore;
|
|||
use MyCodebox\MariaCore\Http\Request;
|
||||
use MyCodebox\MariaCore\Http\Response;
|
||||
|
||||
/**
|
||||
* Class Router
|
||||
*
|
||||
* Lightweight PSR-inspired router with middleware, route groups, named routes, and controller support.
|
||||
*/
|
||||
class Router
|
||||
{
|
||||
/**
|
||||
* @var array
|
||||
* All registered routes, grouped by HTTP method.
|
||||
* @var array<string, array>
|
||||
*/
|
||||
public $routes = [
|
||||
public array $routes = [
|
||||
'GET' => [],
|
||||
'POST' => [],
|
||||
'PUT' => [],
|
||||
'PATCH' => [],
|
||||
'DELETE' => [],
|
||||
'OPTIONS' => [],
|
||||
// Weitere Methoden können ergänzt werden
|
||||
];
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
private $routesAreCached = false;
|
||||
|
||||
|
||||
/**
|
||||
* Named routes for URL generation.
|
||||
* @var array<string, array{method: string, pattern: string}>
|
||||
*/
|
||||
public array $namedRoutes = [];
|
||||
|
||||
|
||||
/**
|
||||
* @var array
|
||||
* Stack for route groups (prefixes and middleware).
|
||||
* @var array<int, array{prefix: string, middleware: array}>
|
||||
*/
|
||||
private array $groupStack = [];
|
||||
|
||||
/**
|
||||
* Global middleware applied to every route.
|
||||
* @var callable[]
|
||||
*/
|
||||
private array $globalMiddleware = [];
|
||||
|
||||
/**
|
||||
* Route registrieren, optional mit Middleware
|
||||
* Register a route with optional middleware.
|
||||
*
|
||||
* @param string $method
|
||||
* @param string $pattern
|
||||
* @param callable $handler
|
||||
* @param callable|string|array $handler
|
||||
* @param array $middleware
|
||||
* @return object RouteRegistrar for method chaining (->name())
|
||||
*/
|
||||
public function add(string $method, string $pattern, $handler, array $middleware = [])
|
||||
public function add(string $method, string $pattern, callable|string|array $handler, array $middleware = [])
|
||||
{
|
||||
$method = strtoupper($method);
|
||||
|
||||
|
|
@ -69,7 +71,7 @@ class Router
|
|||
'name' => null,
|
||||
];
|
||||
|
||||
// Rückgabe eines RouteRegistrar-Objekts für method chaining (z.B. ->name())
|
||||
// Return a RouteRegistrar object for method chaining (e.g. ->name())
|
||||
return new class($this, $method, $pattern) {
|
||||
private $router;
|
||||
private $method;
|
||||
|
|
@ -83,7 +85,6 @@ class Router
|
|||
public function name(string $name)
|
||||
{
|
||||
$this->router->nameRoute($name, $this->method, $this->pattern);
|
||||
// Setze den Namen auch im routes-Array
|
||||
$this->router->routes[$this->method][$this->pattern]['name'] = $name;
|
||||
return $this;
|
||||
}
|
||||
|
|
@ -91,61 +92,60 @@ class Router
|
|||
}
|
||||
|
||||
/**
|
||||
* GET-Route registrieren
|
||||
* Register a GET route.
|
||||
*/
|
||||
public function get(string $pattern, $handler, array $middleware = [])
|
||||
public function get(string $pattern, callable|string|array $handler, array $middleware = [])
|
||||
{
|
||||
return $this->add('GET', $pattern, $handler, $middleware);
|
||||
}
|
||||
|
||||
/**
|
||||
* POST-Route registrieren
|
||||
* Register a POST route.
|
||||
*/
|
||||
public function post(string $pattern, $handler, array $middleware = [])
|
||||
public function post(string $pattern, callable|string|array $handler, array $middleware = [])
|
||||
{
|
||||
return $this->add('POST', $pattern, $handler, $middleware);
|
||||
}
|
||||
|
||||
/**
|
||||
* PUT-Route registrieren
|
||||
* Register a PUT route.
|
||||
*/
|
||||
public function put(string $pattern, $handler, array $middleware = [])
|
||||
public function put(string $pattern, callable|string|array $handler, array $middleware = [])
|
||||
{
|
||||
return $this->add('PUT', $pattern, $handler, $middleware);
|
||||
}
|
||||
|
||||
/**
|
||||
* PATCH-Route registrieren
|
||||
* Register a PATCH route.
|
||||
*/
|
||||
public function patch(string $pattern, $handler, array $middleware = [])
|
||||
public function patch(string $pattern, callable|string|array $handler, array $middleware = [])
|
||||
{
|
||||
return $this->add('PATCH', $pattern, $handler, $middleware);
|
||||
}
|
||||
|
||||
/**
|
||||
* DELETE-Route registrieren
|
||||
* Register a DELETE route.
|
||||
*/
|
||||
public function delete(string $pattern, $handler, array $middleware = [])
|
||||
public function delete(string $pattern, callable|string|array $handler, array $middleware = [])
|
||||
{
|
||||
return $this->add('DELETE', $pattern, $handler, $middleware);
|
||||
}
|
||||
|
||||
/**
|
||||
* OPTIONS-Route registrieren
|
||||
* Register an OPTIONS route.
|
||||
*/
|
||||
public function options(string $pattern, $handler, array $middleware = [])
|
||||
public function options(string $pattern, callable|string|array $handler, array $middleware = [])
|
||||
{
|
||||
return $this->add('OPTIONS', $pattern, $handler, $middleware);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @var callable|null
|
||||
*/
|
||||
private $errorHandler = null;
|
||||
|
||||
/**
|
||||
* Setzt einen globalen Fehler-Handler.
|
||||
* Set a global error handler.
|
||||
* @param callable $handler function(Request $request, \Throwable $e): Response
|
||||
*/
|
||||
public function setErrorHandler(callable $handler): void
|
||||
|
|
@ -154,7 +154,7 @@ class Router
|
|||
}
|
||||
|
||||
/**
|
||||
* Routing ausführen
|
||||
* Dispatch the router for the given request.
|
||||
*
|
||||
* @param Request $request
|
||||
* @return Response
|
||||
|
|
@ -174,7 +174,7 @@ class Router
|
|||
$regex = $this->convertPatternToRegex($pattern, $params);
|
||||
|
||||
if (preg_match($regex, $uri, $matches)) {
|
||||
// Extrahiere Parameter
|
||||
// Extract parameters
|
||||
$params = [];
|
||||
foreach ($matches as $key => $value) {
|
||||
if (!is_int($key)) {
|
||||
|
|
@ -185,13 +185,13 @@ class Router
|
|||
$handler = $route['handler'];
|
||||
$routeMiddleware = $route['middleware'] ?? [];
|
||||
|
||||
// Baue die Middleware-Kette auf
|
||||
// Build middleware stack
|
||||
$middlewareStack = array_merge(
|
||||
$this->globalMiddleware,
|
||||
$routeMiddleware,
|
||||
[
|
||||
function ($request) use ($handler, $params) {
|
||||
// Controller-Support: String "Controller@Methode" oder [ControllerClass, Methode]
|
||||
// Controller support: "Controller@method" or [ControllerClass, method]
|
||||
if (is_string($handler) && strpos($handler, '@') !== false) {
|
||||
[$class, $method] = explode('@', $handler, 2);
|
||||
if (class_exists($class)) {
|
||||
|
|
@ -211,7 +211,7 @@ class Router
|
|||
]
|
||||
);
|
||||
|
||||
// Führe die Middleware-Kette aus
|
||||
// Run middleware stack
|
||||
$runner = function ($request, $middlewareStack, $index = 0) use (&$runner) {
|
||||
if (!isset($middlewareStack[$index])) {
|
||||
throw new \RuntimeException('Middleware stack exhausted with no response.');
|
||||
|
|
@ -231,7 +231,7 @@ class Router
|
|||
if ($this->errorHandler) {
|
||||
return call_user_func($this->errorHandler, $request, $e);
|
||||
}
|
||||
// Fallback: Standard-Fehlerausgabe
|
||||
// Fallback: Standard error output
|
||||
return (new Response())->withJson([
|
||||
'error' => 'Internal Server Error',
|
||||
'message' => $e->getMessage()
|
||||
|
|
@ -240,7 +240,7 @@ class Router
|
|||
}
|
||||
|
||||
/**
|
||||
* Gibt alle registrierten Routen zurück.
|
||||
* Get all registered routes.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
|
|
@ -250,7 +250,7 @@ class Router
|
|||
}
|
||||
|
||||
/**
|
||||
* Gruppiert Routen unter einem gemeinsamen Prefix und/oder Middleware.
|
||||
* Group routes under a common prefix and/or middleware.
|
||||
*
|
||||
* @param string $prefix
|
||||
* @param callable $callback
|
||||
|
|
@ -267,9 +267,8 @@ class Router
|
|||
array_pop($this->groupStack);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Route unter einem Namen registrieren (intern genutzt)
|
||||
* Register a named route (internal use).
|
||||
*/
|
||||
public function nameRoute(string $name, string $method, string $pattern): void
|
||||
{
|
||||
|
|
@ -280,7 +279,7 @@ class Router
|
|||
}
|
||||
|
||||
/**
|
||||
* Generiert eine URL anhand des Routennamens und der Parameter
|
||||
* Generate a URL for a named route and parameters.
|
||||
*
|
||||
* @param string $name
|
||||
* @param array $params
|
||||
|
|
@ -292,7 +291,7 @@ class Router
|
|||
return null;
|
||||
}
|
||||
$pattern = $this->namedRoutes[$name]['pattern'];
|
||||
// Ersetze Platzhalter im Pattern durch Parameter
|
||||
// Replace placeholders in pattern with parameters
|
||||
$url = preg_replace_callback('#\{([a-zA-Z_][a-zA-Z0-9_]*)\}#', function ($matches) use (&$params) {
|
||||
$key = $matches[1];
|
||||
if (isset($params[$key])) {
|
||||
|
|
@ -302,17 +301,15 @@ class Router
|
|||
}
|
||||
return $matches[0];
|
||||
}, $pattern);
|
||||
// Füge übrige Parameter als Query-String an
|
||||
// Add remaining params as query string
|
||||
if (!empty($params)) {
|
||||
$url .= (strpos($url, '?') === false ? '?' : '&') . http_build_query($params);
|
||||
}
|
||||
return $url;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Globale Middleware hinzufügen
|
||||
* Add global middleware.
|
||||
*
|
||||
* @param callable $middleware
|
||||
* @return void
|
||||
|
|
@ -323,7 +320,7 @@ class Router
|
|||
}
|
||||
|
||||
/**
|
||||
* Interne Hilfsmethode: Gibt aktuellen Prefix zurück (aus Gruppenstack)
|
||||
* Get current prefix from group stack.
|
||||
*/
|
||||
private function getCurrentPrefix(): string
|
||||
{
|
||||
|
|
@ -335,7 +332,7 @@ class Router
|
|||
}
|
||||
|
||||
/**
|
||||
* Interne Hilfsmethode: Gibt alle Middleware aus Gruppenstack zurück
|
||||
* Get all middleware from group stack.
|
||||
*/
|
||||
private function getCurrentGroupMiddleware(): array
|
||||
{
|
||||
|
|
@ -348,11 +345,10 @@ class Router
|
|||
return $all;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Wandelt ein Routen-Pattern in einen Regex um und extrahiert Parameternamen
|
||||
* Convert a route pattern to regex and extract parameter names.
|
||||
*
|
||||
* Beispiel: /user/{id} => #^/user/(?P<id>[^/]+)$#
|
||||
* Example: /user/{id} => #^/user/(?P<id>[^/]+)$#
|
||||
*
|
||||
* @param string $pattern
|
||||
* @param array &$params
|
||||
|
|
@ -360,7 +356,7 @@ class Router
|
|||
*/
|
||||
private function convertPatternToRegex(string $pattern, array &$params): string
|
||||
{
|
||||
// Erlaube Constraints: {param:regex}
|
||||
// Allow constraints: {param:regex}
|
||||
$regex = preg_replace_callback(
|
||||
'#\{([a-zA-Z_][a-zA-Z0-9_]*)(:([^}]+))?\}#',
|
||||
function ($matches) use (&$params) {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue