From 7d856c61fbdd43d7d6b90cd19c825362330383b6 Mon Sep 17 00:00:00 2001 From: Pierre Rudloff Date: Thu, 22 Oct 2020 01:00:52 +0200 Subject: [PATCH] Cleaner way to build the redirect --- classes/Controller/BaseController.php | 8 +++++++- classes/Controller/DownloadController.php | 2 +- classes/Controller/FrontController.php | 8 ++++---- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/classes/Controller/BaseController.php b/classes/Controller/BaseController.php index 1922aff..f57b0aa 100644 --- a/classes/Controller/BaseController.php +++ b/classes/Controller/BaseController.php @@ -10,12 +10,12 @@ use Alltube\Config; use Alltube\Library\Downloader; use Alltube\Library\Video; use Alltube\LocaleManager; -use Alltube\SessionFactory; use Aura\Session\Segment; use Consolidation\Log\Logger; use Psr\Container\ContainerInterface; use Slim\Http\Request; use Slim\Http\Response; +use Slim\Router; /** * Abstract class used by every controller. @@ -76,6 +76,11 @@ abstract class BaseController */ protected $logger; + /** + * @var Router + */ + protected $router; + /** * BaseController constructor. * @@ -89,6 +94,7 @@ abstract class BaseController $this->sessionSegment = $session->getSegment(self::class); $this->localeManager = $this->container->get('locale'); $this->downloader = $this->config->getDownloader(); + $this->router = $this->container->get('router'); $this->logger = $this->container->get('logger'); $this->downloader->setLogger($this->logger); diff --git a/classes/Controller/DownloadController.php b/classes/Controller/DownloadController.php index e3c547c..b80890f 100644 --- a/classes/Controller/DownloadController.php +++ b/classes/Controller/DownloadController.php @@ -86,7 +86,7 @@ class DownloadController extends BaseController } } } else { - return $response->withRedirect($this->container->get('router')->pathFor('index')); + return $response->withRedirect($this->router->pathFor('index')); } } diff --git a/classes/Controller/FrontController.php b/classes/Controller/FrontController.php index c6e620b..0cd8efa 100644 --- a/classes/Controller/FrontController.php +++ b/classes/Controller/FrontController.php @@ -13,6 +13,7 @@ use Alltube\Locale; use Alltube\Middleware\CspMiddleware; use Exception; use Slim\Http\StatusCode; +use Slim\Http\Uri; use Symfony\Component\ErrorHandler\ErrorRenderer\HtmlErrorRenderer; use Throwable; use Psr\Container\ContainerInterface; @@ -82,7 +83,7 @@ class FrontController extends BaseController { $this->localeManager->setLocale(new Locale($data['locale'])); - return $response->withRedirect($this->container->get('router')->pathFor('index')); + return $response->withRedirect($this->router->pathFor('index')); } /** @@ -209,14 +210,13 @@ class FrontController extends BaseController if ($this->config->convert && $request->getQueryParam('audio')) { // We skip the info page and get directly to the download. return $response->withRedirect( - $this->container->get('router')->pathFor('download') . - '?' . http_build_query($request->getQueryParams()) + $this->router->pathFor('download', [], $request->getQueryParams()) ); } else { return $this->getInfoResponse($request, $response); } } else { - return $response->withRedirect($this->container->get('router')->pathFor('index')); + return $response->withRedirect($this->router->pathFor('index')); } }