diff --git a/src/Router.php b/src/Router.php index 86e0818..ac9dd3b 100644 --- a/src/Router.php +++ b/src/Router.php @@ -23,6 +23,7 @@ class Router 'PATCH' => [], 'DELETE' => [], 'OPTIONS' => [], + // Weitere HTTP-Methoden können hier hinzugefügt werden ]; /** @@ -368,4 +369,30 @@ class Router ); 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); + } }