Router: interne Forwarding-Methode, kleine Verbesserungen und Kommentare
This commit is contained in:
parent
9c206f52c6
commit
00afdbbc53
1 changed files with 27 additions and 0 deletions
|
|
@ -23,6 +23,7 @@ class Router
|
||||||
'PATCH' => [],
|
'PATCH' => [],
|
||||||
'DELETE' => [],
|
'DELETE' => [],
|
||||||
'OPTIONS' => [],
|
'OPTIONS' => [],
|
||||||
|
// Weitere HTTP-Methoden können hier hinzugefügt werden
|
||||||
];
|
];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -368,4 +369,30 @@ class Router
|
||||||
);
|
);
|
||||||
return '#^' . $regex . '$#';
|
return '#^' . $regex . '$#';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Internally forward to another named route, optionally with parameters and query params.
|
||||||
|
*
|
||||||
|
* @param string $routeName
|
||||||
|
* @param array $params
|
||||||
|
* @param array $queryParams
|
||||||
|
* @param string $method
|
||||||
|
* @return Response
|
||||||
|
*/
|
||||||
|
public function forward(string $routeName, array $params = [], array $queryParams = [], string $method = 'GET'): Response
|
||||||
|
{
|
||||||
|
$url = $this->url($routeName, $params);
|
||||||
|
// Query-String anhängen, falls vorhanden
|
||||||
|
if (!empty($queryParams)) {
|
||||||
|
$url .= (strpos($url, '?') === false ? '?' : '&') . http_build_query($queryParams);
|
||||||
|
}
|
||||||
|
// Neues Request-Objekt mit gewünschter URI und Query-Params erzeugen
|
||||||
|
$forwardedRequest = \MyCodebox\MariaCore\Http\Request::fromGlobalsOverride([
|
||||||
|
'method' => $method,
|
||||||
|
'uri' => strtok($url, '?'),
|
||||||
|
'queryParams' => $queryParams,
|
||||||
|
]);
|
||||||
|
// Dispatch an den Router
|
||||||
|
return $this->dispatch($forwardedRequest);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue