diff --git a/src/Router/src/Router.php b/src/Router/src/Router.php index fdf095c39..7905df506 100644 --- a/src/Router/src/Router.php +++ b/src/Router/src/Router.php @@ -147,7 +147,10 @@ public function import(RoutingConfigurator $routes): void $target = $target->withCore($configurator->core); } - $route = new Route(\ltrim($configurator->pattern, '/'), $target, $configurator->defaults); + $pattern = \str_starts_with($configurator->pattern, '//') + ? $configurator->pattern + : \ltrim($configurator->pattern, '/'); + $route = new Route($pattern, $target, $configurator->defaults); if ($configurator->middleware !== null) { $route = $route->withMiddleware(...$configurator->middleware); diff --git a/src/Router/tests/RouterTest.php b/src/Router/tests/RouterTest.php index 79ada92b4..01ac370bd 100644 --- a/src/Router/tests/RouterTest.php +++ b/src/Router/tests/RouterTest.php @@ -11,7 +11,11 @@ use Spiral\Router\Event\Routing; use Spiral\Router\Exception\RouteNotFoundException; use Spiral\Router\Exception\UndefinedRouteException; +use Spiral\Router\GroupRegistry; +use Spiral\Router\Loader\Configurator\RoutingConfigurator; +use Spiral\Router\Loader\LoaderInterface; use Spiral\Router\Route; +use Spiral\Router\RouteCollection; class RouterTest extends BaseTestCase { @@ -75,4 +79,19 @@ public function testRouteNotFoundEventShouldBeDispatched(): void $this->expectException(RouteNotFoundException::class); $router->handle($request); } + + public function testImportWithHost(): void + { + $router = $this->makeRouter('https://host.com', $this->createMock(EventDispatcherInterface::class)); + + $configurator = new RoutingConfigurator(new RouteCollection(), $this->createMock(LoaderInterface::class)); + $configurator->add('foo', '///register')->callable(fn () => null); + + $router->import($configurator); + $this->container->get(GroupRegistry::class)->registerRoutes($router); + + $uri = (string) $router->uri('foo', ['host' => 'some']); + $this->assertSame('some/register', $uri); + $this->assertFalse(\str_contains('https://host.com', $uri)); + } }