content-type 定義了瀏覽器把獲取的文件當(dāng)什么運(yùn)行,例如一個(gè)javascript文件,設(shè)置為
text/html就不給運(yùn)行了,必須在Response Header中指定正確兼容的才能被運(yùn)行。
例如,我的多個(gè)css文件都是PHP動態(tài)生成的,如果直接
... # in a method of a class extending think\Controller
return $this->fetch("/style/$name.css",[
'color' => 'rgb(252,255,2)',
'border-radius' => '.34rem'
'width' => 750px',
'bg_img' => 'http://website.com/favicon.ico'
]);
...
可以設(shè)置config.php中為css,但是所有的html文件都會被識別為css,所以不能動配置文件中的
solution
不使用默認(rèn)的response配置,而是動態(tài)更新一些配置,如下
$content = $this->view->fetch("/style/$name.css", [
'color' => 'rgb(252,255,2)',
'border-radius' => '.34rem'
'width' => 750px',
'bg_img' => 'http://website.com/favicon.ico'
]);
# fetch 后傳給 Response 作為輸出內(nèi)容,不直接 return
$res = new Response;
$res->content($content)->contentType('text/css')->send();
solution 2
可以使用less,scss,sass等來預(yù)處理呀,but ....