LinkHeaderMiddleware should use the same URL as ViewFactory
This way the X-Forwarded-Path header is used to generate the Link header
This commit is contained in:
parent
bfaea0e381
commit
9410d4b49b
3 changed files with 31 additions and 30 deletions
|
@ -62,7 +62,7 @@ class App extends \Slim\App
|
|||
// Middlewares.
|
||||
$this->add(new LocaleMiddleware($container));
|
||||
$this->add(new CspMiddleware($container));
|
||||
$this->add(new LinkHeaderMiddleware($container));
|
||||
$this->add(new LinkHeaderMiddleware());
|
||||
$this->add(new RouterPathMiddleware($container));
|
||||
|
||||
// Controllers.
|
||||
|
|
|
@ -57,22 +57,13 @@ class ViewFactory
|
|||
}
|
||||
|
||||
/**
|
||||
* Create Smarty view object.
|
||||
* Create a URI suitable for templates.
|
||||
*
|
||||
* @param ContainerInterface $container Slim dependency container
|
||||
* @param Request|null $request PSR-7 request
|
||||
*
|
||||
* @return Smarty
|
||||
* @throws SmartyException
|
||||
* @param Request $request
|
||||
* @return Uri
|
||||
*/
|
||||
public static function create(ContainerInterface $container, Request $request = null): Smarty
|
||||
public static function prepareUri(Request $request): Uri
|
||||
{
|
||||
if (!isset($request)) {
|
||||
$request = $container->get('request');
|
||||
}
|
||||
|
||||
$view = new Smarty($container->get('root_path') . '/templates/');
|
||||
|
||||
/** @var Uri $uri */
|
||||
$uri = $request->getUri();
|
||||
if (in_array('https', $request->getHeader('X-Forwarded-Proto'))) {
|
||||
|
@ -92,11 +83,31 @@ class ViewFactory
|
|||
$uri = $uri->withBasePath($path);
|
||||
}
|
||||
|
||||
return self::cleanBasePath($uri);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create Smarty view object.
|
||||
*
|
||||
* @param ContainerInterface $container Slim dependency container
|
||||
* @param Request|null $request PSR-7 request
|
||||
*
|
||||
* @return Smarty
|
||||
* @throws SmartyException
|
||||
*/
|
||||
public static function create(ContainerInterface $container, Request $request = null): Smarty
|
||||
{
|
||||
if (!isset($request)) {
|
||||
$request = $container->get('request');
|
||||
}
|
||||
|
||||
$view = new Smarty($container->get('root_path') . '/templates/');
|
||||
|
||||
$uri = self::prepareUri($request);
|
||||
|
||||
/** @var LocaleManager $localeManager */
|
||||
$localeManager = $container->get('locale');
|
||||
|
||||
$uri = self::cleanBasePath($uri);
|
||||
|
||||
$smartyPlugins = new SmartyPlugins($container->get('router'), $uri->withUserInfo(''));
|
||||
$view->registerPlugin('function', 'path_for', [$smartyPlugins, 'pathFor']);
|
||||
$view->registerPlugin('function', 'base_url', [$smartyPlugins, 'baseUrl']);
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
namespace Alltube\Middleware;
|
||||
|
||||
use Alltube\Factory\ViewFactory;
|
||||
use Psr\Container\ContainerInterface;
|
||||
use Slim\Http\Request;
|
||||
use Slim\Http\Response;
|
||||
|
@ -13,19 +14,6 @@ use Slim\Router;
|
|||
*/
|
||||
class LinkHeaderMiddleware
|
||||
{
|
||||
/**
|
||||
* @var Router
|
||||
*/
|
||||
private $router;
|
||||
|
||||
/**
|
||||
* LinkHeaderMiddleware constructor.
|
||||
* @param ContainerInterface $container
|
||||
*/
|
||||
public function __construct(ContainerInterface $container)
|
||||
{
|
||||
$this->router = $container->get('router');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Request $request
|
||||
|
@ -35,12 +23,14 @@ class LinkHeaderMiddleware
|
|||
*/
|
||||
public function __invoke(Request $request, Response $response, callable $next)
|
||||
{
|
||||
$uri = ViewFactory::prepareUri($request);
|
||||
|
||||
$response = $response->withHeader(
|
||||
'Link',
|
||||
implode(
|
||||
'; ',
|
||||
[
|
||||
'<' . $this->router->getBasePath() . '/css/style.css>',
|
||||
'<' . $uri->getBasePath() . '/css/style.css>',
|
||||
'rel=preload', 'as=style'
|
||||
]
|
||||
)
|
||||
|
|
Loading…
Reference in a new issue