content-type 定義了瀏覽器把獲取的文件當(dāng)什么運行,例如一個javascript文件方庭,設(shè)置為
text/html
就不給運行了,必須在Response Header中指定正確兼容的才能被運行趟卸。
例如树绩,我的多個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 ....