diff --git a/src/Router.php b/src/Router.php index d3f40d4..03a0d0d 100644 --- a/src/Router.php +++ b/src/Router.php @@ -187,6 +187,8 @@ public function allowedMethodsForPath(string $path): array * A URI is redirectable if it can be handled by a GET route within the * router. The URI can either be a URL containing the application domain, * or an absolute path. + * + * The ?query and #fragment parts, if any, are ignored. */ public function isRedirectable(string $uri): bool { @@ -196,12 +198,22 @@ public function isRedirectable(string $uri): bool $uri = substr($uri, strlen($base_url)); } - if (str_starts_with($uri, '/')) { - $allowed_methods = $this->allowedMethodsForPath($uri); - return in_array('GET', $allowed_methods); - } else { + if (!str_starts_with($uri, '/')) { return false; } + + $position_hash = strpos($uri, '#'); + if ($position_hash !== false) { + $uri = substr($uri, 0, $position_hash); + } + + $position_query = strpos($uri, '?'); + if ($position_query !== false) { + $uri = substr($uri, 0, $position_query); + } + + $allowed_methods = $this->allowedMethodsForPath($uri); + return in_array('GET', $allowed_methods); } /** diff --git a/tests/RouterTest.php b/tests/RouterTest.php index de557e7..c8bf21b 100644 --- a/tests/RouterTest.php +++ b/tests/RouterTest.php @@ -264,6 +264,26 @@ public function testIsRedirectableWithNotSupportedUrl(): void $this->assertFalse($is_redirectable); } + public function testIsRedirectableWithQueryPart(): void + { + $router = new Router(); + $router->addRoute('GET', '/rabbits/new', 'rabbits#new'); + + $is_redirectable = $router->isRedirectable('/rabbits/new?foo=bar'); + + $this->assertTrue($is_redirectable); + } + + public function testIsRedirectableWithFragmentPart(): void + { + $router = new Router(); + $router->addRoute('GET', '/rabbits/new', 'rabbits#new'); + + $is_redirectable = $router->isRedirectable('/rabbits/new#foo'); + + $this->assertTrue($is_redirectable); + } + public function testUriByName(): void { $router = new Router();