首页 > 解决方案 > AltoRouter 发送 Mime 类型

问题描述

AltoRouter用来将我的网址路由到正确的文件。现在基本上在这里,我已经描述了我的问题。

在一页上,我有一个警报,由引导程序设计。

它的定义很简单:

$('#wrongPasswordDiv').html('<br/><div class="alert alert-danger" id="wrongPWAlert" role="alert">Falsches Passwort. Bitte erneut versuchen!</div>');

此外,之前还包含 Bootstrap css 文件:

<link rel="stylesheet" type="text/css" href="/bootstrapcss" />

bootstrapcssAltoRouter使用这行代码路由到正确的 css 文件:

$router->map('GET','/bootstrapcss','vendor/twbs/bootstrap/dist/css/bootstrap.css','bootstrapcss');

现在,在控制台中,它会抛出一个警告说Resource interpreted as Stylesheet but transferred with MIME type text/html: "http://localhost/bootstrapcss"

如果我使用 css 文件的完整路径、CDN 或删除DOCTYPE,它的工作正常。但我不想做这些变化中的任何一个......删除文档类型,可能会损坏其他功能,如果我使用完整的css路径,那么我不需要路由......

有什么想法可以发送Content-type: text/css标头以使其正常工作吗?

标签: phphtmlcssurl-routingaltorouter

解决方案


您应该Content-Type在发送响应内容之前发送正确的内容。我不太了解 PHP,所以我可能不会以最好的方式阅读 CSS,但这是一个工作示例:

使用这条路线:

$router->map('GET','/bootstrapcss','example.css','bootstrapcss');

然后在匹配时:

$match = $router->match();

if($match['name'] === 'bootstrapcss'){
    header("Content-Type: text/css");
    $fileName = $match['target'];
    echo file_get_contents($fileName);
    return;
}

对于上下文,我在这里有一个完整的示例:https
://gist.github.com/kobi/09eaeeecb3406b193a84a674218798a9 这是基于 AltoRouter 上的基本示例:https ://github.com/dannyvankooten/AltoRouter/tree/master/examples /基本的


推荐阅读