Use middleware to set basepath from X-Forwarded-Path header

This commit is contained in:
bellington3 2020-10-18 18:18:51 +02:00
parent 24f8ac0dbd
commit 5c81a19233
2 changed files with 51 additions and 8 deletions

View File

@ -0,0 +1,42 @@
<?php
namespace Alltube;
use Psr\Container\ContainerInterface;
use Slim\Http\Request;
use Slim\Http\Response;
use Slim\Interfaces\RouterInterface;
/**
* Class RouterPathMiddleware
* @package Alltube
*/
class RouterPathMiddleware
{
/**
* @var RouterInterface
*/
private $router;
/**
* RouterPathMiddleware constructor.
* @param ContainerInterface $container
*/
public function __construct(ContainerInterface $container)
{
$this->router = $container->get('router');
}
/**
* @param Request $request
* @param Response $response
* @param callable $next
* @return mixed
*/
public function __invoke(Request $request, Response $response, callable $next)
{
$this->router->setBasePath(current($request->getHeader('X-Forwarded-Path')));
return $next($request, $response);
}
}

View File

@ -9,6 +9,7 @@ use Alltube\Controller\JsonController;
use Alltube\LocaleManagerFactory;
use Alltube\LocaleMiddleware;
use Alltube\LoggerFactory;
use Alltube\RouterPathMiddleware;
use Alltube\ViewFactory;
use Slim\App;
use Slim\Container;
@ -32,6 +33,7 @@ try {
$container['locale'] = LocaleManagerFactory::create();
$app->add(new LocaleMiddleware($container));
$app->add(new RouterPathMiddleware($container));
// Smarty.
$container['view'] = ViewFactory::create($container);
@ -51,39 +53,38 @@ try {
$container['notAllowedHandler'] = [$frontController, 'notAllowed'];
// Routes.
$basePath = current($container->get('request')->getHeader('X-Forwarded-Path'));
$app->get(
$basePath . '/',
'/',
[$frontController, 'index']
)->setName('index');
$app->get(
$basePath . '/extractors',
'/extractors',
[$frontController, 'extractors']
)->setName('extractors');
$app->any(
$basePath . '/info',
'/info',
[$frontController, 'info']
)->setName('info');
$app->any(
$basePath . '/watch',
'/watch',
[$frontController, 'info']
);
$app->any(
$basePath . '/download',
'/download',
[$downloadController, 'download']
)->setName('download');
$app->get(
$basePath . '/locale/{locale}',
'/locale/{locale}',
[$frontController, 'locale']
)->setName('locale');
$app->get(
$basePath . '/json',
'/json',
[$jsonController, 'json']
)->setName('json');