diff --git a/src/RouteCollection.php b/src/RouteCollection.php index 629ed549..ed3f691a 100644 --- a/src/RouteCollection.php +++ b/src/RouteCollection.php @@ -17,6 +17,8 @@ */ final class RouteCollection implements RouteCollectionInterface { + private string $uriPrefix = ''; + /** * @psalm-var Items */ @@ -55,6 +57,16 @@ public function getRouteTree(bool $routeAsString = true): array return $this->buildTree($this->items, $routeAsString); } + public function getUriPrefix(): string + { + return $this->uriPrefix; + } + + public function setUriPrefix(string $prefix): void + { + $this->uriPrefix = $prefix; + } + private function ensureItemsInjected(): void { if ($this->items === []) { @@ -83,10 +95,11 @@ private function injectItems(array $items): void private function injectItem(Group|Route $route): void { if ($route instanceof Group) { - $this->injectGroup($route, $this->items); + $this->injectGroup($route, $this->items, $this->uriPrefix); return; } + $route = $route->pattern($this->uriPrefix . $route->getData('pattern')); $routeName = $route->getData('name'); $this->items[] = $routeName; if (isset($this->routes[$routeName]) && !$route->getData('override')) { diff --git a/src/RouteCollectionInterface.php b/src/RouteCollectionInterface.php index fe2ff1f7..b7306560 100644 --- a/src/RouteCollectionInterface.php +++ b/src/RouteCollectionInterface.php @@ -6,6 +6,18 @@ interface RouteCollectionInterface { + /** + * Returns URI prefix. + * + * @see setUriPrefix() + */ + public function getUriPrefix(): string; + + /** + * Sets the URI prefix so that all routes are registered to this path after the domain part. + */ + public function setUriPrefix(string $prefix): void; + /** * @return Route[] */ diff --git a/src/UrlGeneratorInterface.php b/src/UrlGeneratorInterface.php index 214e8910..f7c0e6ed 100644 --- a/src/UrlGeneratorInterface.php +++ b/src/UrlGeneratorInterface.php @@ -60,10 +60,6 @@ public function generateFromCurrent( ?string $fallbackRouteName = null ): string; - public function getUriPrefix(): string; - - public function setUriPrefix(string $name): void; - /** * Set default argument value. * diff --git a/tests/RouteCollectionTest.php b/tests/RouteCollectionTest.php index 99b0ca1e..672fa034 100644 --- a/tests/RouteCollectionTest.php +++ b/tests/RouteCollectionTest.php @@ -28,6 +28,25 @@ final class RouteCollectionTest extends TestCase { + public function testUriPrefix(): void + { + $route1 = Route::get('/')->name('route1'); + $route2 = Route::get('/{id}')->name('route2'); + + $group = Group::create()->routes($route1); + + $collector = new RouteCollector(); + $collector->addGroup($group); + $collector->addRoute($route2); + + $routeCollection = new RouteCollection($collector); + $routeCollection->setUriPrefix($prefix = '/api'); + + $this->assertSame($prefix, $routeCollection->getUriPrefix()); + $this->assertStringStartsWith($prefix, $routeCollection->getRoute('route1')->getData('pattern')); + $this->assertStringStartsWith($prefix, $routeCollection->getRoute('route2')->getData('pattern')); + } + public function testAddRouteWithDuplicateName(): void { $listRoute = Route::get('/')->name('my-route');