diff --git a/CHANGELOG.md b/CHANGELOG.md index ed318e1..54ea5f5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file. This projec to [Semantic Versioning] (http://semver.org/). For change log format, use [Keep a Changelog] (http://keepachangelog.com/). +## [2.5.0] - 2024-12-03 + +### Added + +- `Router::finalizePath()` to finalize path + ## [2.4.1] - 2024-12-03 ### Fixed diff --git a/src/Router.php b/src/Router.php index 504f735..d5c5529 100644 --- a/src/Router.php +++ b/src/Router.php @@ -109,18 +109,32 @@ public function generate(string|RouteInterface $route, array|RouteAttributes $pa throw new NotFoundException(sprintf('Route "%s" does not exists', $route)); } - $str = $route->generate($parameters); + return $this->finalizePath($route->generate($parameters)); + } + + /** + * Finalize path. + * + * @param string $path + * + * @return string + */ + public function finalizePath(string $path): string + { + if (true === str_contains($path, '://')) { + return $path; + } // X-Forwarded-Prefix if (false !== $this->options['X-Forwarded-Prefix']) { $xForwardedPrefix = $this->options['X-Forwarded-Prefix'] === true ? 'X-Forwarded-Prefix' : (string)$this->options['X-Forwarded-Prefix']; $xForwardedPrefix = 'HTTP_' . strtoupper(str_replace('-', '_', $xForwardedPrefix)); if (!empty($prefix = $_SERVER[$xForwardedPrefix] ?? null)) { - $str = '/' . trim($prefix, '/') . $str; + $path = rtrim('/' . trim($prefix, '/'), '/') . '/' . ltrim($path, '/'); } } - return $str; + return $path; } private function generateParameters(array|RouteAttributes $parameters = []): array @@ -199,4 +213,4 @@ public function handle(ServerRequestInterface &$request): ?RouteInterface return $route; } -} \ No newline at end of file +}