Set Link header in PHP so it can work in a subfolder
This commit is contained in:
parent
bd61320cfc
commit
496ac212e2
3 changed files with 48 additions and 1 deletions
|
@ -36,5 +36,4 @@ FileETag None
|
||||||
Header set X-Content-Type-Options nosniff
|
Header set X-Content-Type-Options nosniff
|
||||||
Header set X-XSS-Protection "1; mode=block"
|
Header set X-XSS-Protection "1; mode=block"
|
||||||
Header set Referrer-Policy no-referrer
|
Header set Referrer-Policy no-referrer
|
||||||
Header add Link "</css/style.css>; rel=preload" "expr=%{CONTENT_TYPE} =~ m#text/html#"
|
|
||||||
</ifmodule>
|
</ifmodule>
|
||||||
|
|
46
classes/LinkHeaderMiddleware.php
Normal file
46
classes/LinkHeaderMiddleware.php
Normal file
|
@ -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\FrontController;
|
||||||
use Alltube\Controller\JsonController;
|
use Alltube\Controller\JsonController;
|
||||||
use Alltube\CspMiddleware;
|
use Alltube\CspMiddleware;
|
||||||
use Alltube\ErrorHandler;
|
use Alltube\ErrorHandler;
|
||||||
|
use Alltube\LinkHeaderMiddleware;
|
||||||
use Alltube\LocaleManagerFactory;
|
use Alltube\LocaleManagerFactory;
|
||||||
use Alltube\LocaleMiddleware;
|
use Alltube\LocaleMiddleware;
|
||||||
use Alltube\LoggerFactory;
|
use Alltube\LoggerFactory;
|
||||||
|
@ -44,6 +45,7 @@ try {
|
||||||
$app->add(new LocaleMiddleware($container));
|
$app->add(new LocaleMiddleware($container));
|
||||||
$app->add(new RouterPathMiddleware($container));
|
$app->add(new RouterPathMiddleware($container));
|
||||||
$app->add(new CspMiddleware($container));
|
$app->add(new CspMiddleware($container));
|
||||||
|
$app->add(new LinkHeaderMiddleware($container));
|
||||||
|
|
||||||
// Controllers.
|
// Controllers.
|
||||||
$frontController = new FrontController($container);
|
$frontController = new FrontController($container);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue