Skip to content
Permalink
Browse files
Set Link header in PHP so it can work in a subfolder
  • Loading branch information
Rudloff committed Oct 20, 2020
1 parent bd61320 commit 496ac212e2f0f25575e3cf4164f8328dcdf2787f
Showing 3 changed files with 48 additions and 1 deletion.
@@ -36,5 +36,4 @@ FileETag None
Header set X-Content-Type-Options nosniff
Header set X-XSS-Protection "1; mode=block"
Header set Referrer-Policy no-referrer
Header add Link "</css/style.css>; rel=preload" "expr=%{CONTENT_TYPE} =~ m#text/html#"
</ifmodule>
@@ -0,0 +1,46 @@
<?php

namespace Alltube;

use Psr\Container\ContainerInterface;
use Slim\Http\Request;
use Slim\Http\Response;
use Slim\Router;

/**
* Class LinkHeaderMiddleware
* @package Alltube
*/
class LinkHeaderMiddleware
{
/**
* @var Router
*/
private $router;

/**
* LinkHeaderMiddleware 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)
{
$response = $response->withHeader(
'Link',
'<' . $this->router->getBasePath() . '/css/style.css>; rel=preload; as=style'
);


return $next($request, $response);
}
}
@@ -8,6 +8,7 @@
use Alltube\Controller\JsonController;
use Alltube\CspMiddleware;
use Alltube\ErrorHandler;
use Alltube\LinkHeaderMiddleware;
use Alltube\LocaleManagerFactory;
use Alltube\LocaleMiddleware;
use Alltube\LoggerFactory;
@@ -44,6 +45,7 @@
$app->add(new LocaleMiddleware($container));
$app->add(new RouterPathMiddleware($container));
$app->add(new CspMiddleware($container));
$app->add(new LinkHeaderMiddleware($container));

// Controllers.
$frontController = new FrontController($container);

0 comments on commit 496ac21

Please sign in to comment.