diff --git a/Slim/Routing/Dispatcher.php b/Slim/Routing/Dispatcher.php index 0318accb3..55e7f56a1 100644 --- a/Slim/Routing/Dispatcher.php +++ b/Slim/Routing/Dispatcher.php @@ -38,8 +38,10 @@ protected function createDispatcher(): FastRouteDispatcher } $routeDefinitionCallback = function (RouteCollector $r) { + $basePath = $this->routeCollector->getBasePath(); + foreach ($this->routeCollector->getRoutes() as $route) { - $r->addRoute($route->getMethods(), $route->getPattern(), $route->getIdentifier()); + $r->addRoute($route->getMethods(), $basePath . $route->getPattern(), $route->getIdentifier()); } }; diff --git a/Slim/Routing/RouteCollectorProxy.php b/Slim/Routing/RouteCollectorProxy.php index 695c46c7a..d4ddb1d1d 100644 --- a/Slim/Routing/RouteCollectorProxy.php +++ b/Slim/Routing/RouteCollectorProxy.php @@ -43,27 +43,27 @@ class RouteCollectorProxy implements RouteCollectorProxyInterface /** * @var string */ - protected $basePath; + protected $groupPattern; /** * @param ResponseFactoryInterface $responseFactory * @param CallableResolverInterface $callableResolver * @param RouteCollectorInterface|null $routeCollector * @param ContainerInterface|null $container - * @param string $basePath + * @param string $groupPattern */ public function __construct( ResponseFactoryInterface $responseFactory, CallableResolverInterface $callableResolver, ?ContainerInterface $container = null, ?RouteCollectorInterface $routeCollector = null, - string $basePath = '' + string $groupPattern = '' ) { $this->responseFactory = $responseFactory; $this->callableResolver = $callableResolver; $this->container = $container; $this->routeCollector = $routeCollector ?? new RouteCollector($responseFactory, $callableResolver, $container); - $this->basePath = $basePath; + $this->groupPattern = $groupPattern; } /** @@ -103,7 +103,7 @@ public function getRouteCollector(): RouteCollectorInterface */ public function getBasePath(): string { - return $this->basePath; + return $this->routeCollector->getBasePath(); } /** @@ -111,7 +111,8 @@ public function getBasePath(): string */ public function setBasePath(string $basePath): RouteCollectorProxyInterface { - $this->basePath = $basePath; + $this->routeCollector->setBasePath($basePath); + return $this; } @@ -176,7 +177,7 @@ public function any(string $pattern, $callable): RouteInterface */ public function map(array $methods, string $pattern, $callable): RouteInterface { - $pattern = $this->basePath . $pattern; + $pattern = $this->groupPattern . $pattern; return $this->routeCollector->map($methods, $pattern, $callable); } @@ -186,7 +187,8 @@ public function map(array $methods, string $pattern, $callable): RouteInterface */ public function group(string $pattern, $callable): RouteGroupInterface { - $pattern = $this->basePath . $pattern; + $pattern = $this->groupPattern . $pattern; + return $this->routeCollector->group($pattern, $callable); } diff --git a/tests/Routing/RouteCollectorProxyTest.php b/tests/Routing/RouteCollectorProxyTest.php index 70f6c8430..6564bdfde 100644 --- a/tests/Routing/RouteCollectorProxyTest.php +++ b/tests/Routing/RouteCollectorProxyTest.php @@ -100,16 +100,15 @@ public function testGetSetBasePath() $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $callableResolverProphecy = $this->prophesize(CallableResolverInterface::class); $containerProphecy = $this->prophesize(ContainerInterface::class); - $routeCollectorProphecy = $this->prophesize(RouteCollectorInterface::class); $routeCollectorProxy = new RouteCollectorProxy( $responseFactoryProphecy->reveal(), $callableResolverProphecy->reveal(), - $containerProphecy->reveal(), - $routeCollectorProphecy->reveal(), - $basePath + $containerProphecy->reveal() ); + $routeCollectorProxy->setBasePath($basePath); + $this->assertEquals($basePath, $routeCollectorProxy->getBasePath()); $newBasePath = '/new/base/path';