Cleaner way to build the redirect

This commit is contained in:
Pierre Rudloff 2020-10-22 01:00:52 +02:00
parent 7dead957f0
commit 7d856c61fb
3 changed files with 12 additions and 6 deletions

View file

@ -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);

View file

@ -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'));
}
}

View file

@ -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'));
}
}