From 26f4d05e8731ef7b313cd8b9e9c49894d0f7da95 Mon Sep 17 00:00:00 2001 From: Matias Griese Date: Fri, 11 Dec 2020 15:32:09 +0200 Subject: [PATCH] Fixed dropped query params when `?` is preceded with `/` [#2964] --- CHANGELOG.md | 1 + .../Types/Pages/Traits/PageRoutableTrait.php | 3 +- .../Common/Processors/InitializeProcessor.php | 33 +++++++++---------- .../Flex/Pages/Traits/PageRoutableTrait.php | 2 +- 4 files changed, 19 insertions(+), 20 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index db5a65e26..82e8913df 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ * Fixed broken check if php exif module is enabled in `ImageFile::fixOrientation()` * Fixed `StaticResizeTrait::resize()` bad image height/width attributes if `null` values are passed to the method * Fixed twig script/style tag `{% script 'file.js' at 'bottom' %}`, replaces broken `in` operator [#3084](https://github.com/getgrav/grav/issues/3084) + * Fixed dropped query params when `?` is preceded with `/` [#2964](https://github.com/getgrav/grav/issues/2964) # v1.7.0-rc.19 ## 12/02/2020 diff --git a/system/src/Grav/Common/Flex/Types/Pages/Traits/PageRoutableTrait.php b/system/src/Grav/Common/Flex/Types/Pages/Traits/PageRoutableTrait.php index 27dcf5bef..e2045ecef 100644 --- a/system/src/Grav/Common/Flex/Types/Pages/Traits/PageRoutableTrait.php +++ b/system/src/Grav/Common/Flex/Types/Pages/Traits/PageRoutableTrait.php @@ -98,12 +98,13 @@ trait PageRoutableTrait { $grav = Grav::instance(); $uri = $grav['uri']; + /** @var Pages $pages */ $pages = $grav['pages']; $uri_path = rtrim(urldecode($uri->path()), '/'); $routes = $pages->routes(); if (isset($routes[$uri_path])) { - $page = $pages->dispatch($uri->route(), false, false); + $page = $pages->find($uri->route()); /** @var PageInterface|null $child_page */ $child_page = $page ? $page->parent() : null; while ($child_page && !$child_page->root()) { diff --git a/system/src/Grav/Common/Processors/InitializeProcessor.php b/system/src/Grav/Common/Processors/InitializeProcessor.php index 3bbde95cf..661b1bf8a 100644 --- a/system/src/Grav/Common/Processors/InitializeProcessor.php +++ b/system/src/Grav/Common/Processors/InitializeProcessor.php @@ -25,6 +25,7 @@ use Grav\Framework\Session\Exceptions\SessionException; use Monolog\Formatter\LineFormatter; use Monolog\Handler\SyslogHandler; use Monolog\Logger; +use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\RequestHandlerInterface; @@ -100,14 +101,16 @@ class InitializeProcessor extends ProcessorBase $this->initializePages($config); // Initialize URI. - $uri = $this->initializeUri($config); + $this->initializeUri($config); // Grav may return redirect response right away. - $response = $this->handleRedirectRequest($config, $uri); - if ($response) { - $this->stopTimer('_init'); + if ($config->get('system.pages.redirect_trailing_slash', false)) { + $response = $this->handleRedirectRequest($request); + if ($response) { + $this->stopTimer('_init'); - return $response; + return $response; + } } // Load accounts (decides class to be used). @@ -363,7 +366,7 @@ class InitializeProcessor extends ProcessorBase } - protected function initializeUri(Config $config): Uri + protected function initializeUri(Config $config): void { $this->startTimer('_init_uri', 'Initialize URI'); @@ -374,23 +377,17 @@ class InitializeProcessor extends ProcessorBase $uri->init(); $this->stopTimer('_init_uri'); - - return $uri; } - protected function handleRedirectRequest(Config $config, Uri $uri): ?ResponseInterface + protected function handleRedirectRequest(RequestInterface $request): ?ResponseInterface { - $grav = $this->container; - // Redirect pages with trailing slash if configured to do so. - $path = $uri->path() ?: '/'; - if ($path !== '/' - && $config->get('system.pages.redirect_trailing_slash', false) - && Utils::endsWith($path, '/') - ) { - $redirect = $uri::getCurrentRoute()->toString(); + $uri = $request->getUri(); + $path = $request->getUri()->getPath() ?: '/'; - return $grav->getRedirectResponse($redirect); + if ($path !== '/' && Utils::endsWith($path, '/') + ) { + return $this->container->getRedirectResponse((string)$uri->withPath(rtrim($path, '/'))); } return null; diff --git a/system/src/Grav/Framework/Flex/Pages/Traits/PageRoutableTrait.php b/system/src/Grav/Framework/Flex/Pages/Traits/PageRoutableTrait.php index 5d5b25428..9b02572d2 100644 --- a/system/src/Grav/Framework/Flex/Pages/Traits/PageRoutableTrait.php +++ b/system/src/Grav/Framework/Flex/Pages/Traits/PageRoutableTrait.php @@ -504,7 +504,7 @@ trait PageRoutableTrait if (isset($routes[$uri_path])) { /** @var PageInterface $child_page|null */ - $child_page = $pages->dispatch($uri->route())->parent(); + $child_page = $pages->find($uri->route())->parent(); if (null !== $child_page) { while (!$child_page->root()) { if ($this->path() === $child_page->path()) {