webman處理靜態(tài)文件
webman支持靜態(tài)文件訪問,靜態(tài)文件都放置于public
目錄下,例如訪問 http://127.0.0.8787/upload/avatar.png
實(shí)際上是訪問{主項(xiàng)目目錄}/public/upload/avatar.png
。
注意
以/app/xx/文件名
開頭的靜態(tài)文件訪問實(shí)際是訪問應(yīng)用插件的public
目錄,也就是說不支持{主項(xiàng)目目錄}/public/app/
下的目錄訪問。
更多請參考應(yīng)用插件
關(guān)閉靜態(tài)文件支持
如果不需要靜態(tài)文件支持,打開config/static.php
將enable
選項(xiàng)改成false。關(guān)閉后所有靜態(tài)文件的訪問會返回404。
更改靜態(tài)文件目錄
webman默認(rèn)使用public目錄為靜態(tài)文件目錄。如需修改請更改support/helpers.php
的中的public_path()
助手函數(shù)。
靜態(tài)文件中間件
webman自帶一個(gè)靜態(tài)文件中間件,位置app/middleware/StaticFile.php
。
有時(shí)我們需要對靜態(tài)文件做一些處理,例如給靜態(tài)文件增加跨域http頭,禁止訪問以點(diǎn)(.
)開頭的文件可以使用這個(gè)中間件
app/middleware/StaticFile.php
內(nèi)容類似如下:
<?php
namespace support\middleware;
use Webman\MiddlewareInterface;
use Webman\Http\Response;
use Webman\Http\Request;
class StaticFile implements MiddlewareInterface
{
public function process(Request $request, callable $next) : Response
{
// 禁止訪問.開頭的隱藏文件
if (strpos($request->path(), '/.') !== false) {
return response('<h1>403 forbidden</h1>', 403);
}
/** @var Response $response */
$response = $next($request);
// 增加跨域http頭
/*$response->withHeaders([
'Access-Control-Allow-Origin' => '*',
'Access-Control-Allow-Credentials' => 'true',
]);*/
return $response;
}
}
如果需要此中間件時(shí),需要到config/static.php
中middleware
選項(xiàng)中開啟。