国产+高潮+在线,国产 av 仑乱内谢,www国产亚洲精品久久,51国产偷自视频区视频,成人午夜精品网站在线观看

關(guān)于webman圖片,css等路徑問題

siwei

問題描述

請教:關(guān)于webman圖片,css等路徑問題
截圖

結(jié)果:圖片,css 都不能正確顯示。
截圖

1192 2 0
2個回答

Madman

1、因為網(wǎng)站根目錄實際是public,所以如果想直接訪問靜態(tài)資源可以放到public目錄下。

2、如果是插件應(yīng)用的話,支持放到plugin/{插件}/public目錄下,比如這里的插件名為foo,那么請求地址就是http://127.0.0.1:8787/app/foo/avatar.png,實際上是獲取plugin/foo/public/avatar.png文件。
參考 http://www.wtbis.cn/doc/webman/app/static.html

3、如果實在想根據(jù)自己的目錄來存放靜態(tài)資源,可以通過設(shè)置路由的方法實現(xiàn)。
新建一個控制器PublicController.php如下:

<?php

declare(strict_types=1);

namespace app\controller;

use support\Request;
use support\Response;

class PublicController
{
    /**
     * @param Request $request
     * @param string $path
     * @return Response
     */
    public function index(Request $request, string $path = ''): Response
    {
        // 靜態(tài)文件目錄
        $static_base_path = base_path() . '/app/view/assets';
        // 安全檢查,避免url里 /../../../password 這樣的非法訪問
        if (str_contains($path, '..')) return not_found();
        // 文件
        $file = "$static_base_path/$path";
        if (!is_file($file)) return not_found();
        return response('')->withFile($file);
    }
}

然后在config/route.php里加一條路由

Route::any('/app/view/assets/[{path:.+}]', [\app\controller\PublicController::class, 'index']);

這個時候就可以訪問指定目錄下的靜態(tài)資源了

  • Madman 2024-02-18

    模板里不需要獲取啊,直接像你那樣寫地址就行,實在不行加個配置文件 把這個路徑寫到配置文件里,再讀取配置文件里的參數(shù)

  • siwei 2024-02-18

    html 模版怎么調(diào)用 $static_base_path 比如如下模板
    <!doctype html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>webman</title>
    </head>
    <body>
    hello {$name}
    </body>
    </html>

  • Madman 2024-02-18

    我還是沒明白你模板里要這個變量是干嘛用,如果只是為了寫資源路徑簡短一點,完全可以在app/function.php里加一個方法函數(shù),比如

    function static_base_path(string $path = '')
    {
        $static_base_path = 'app/view/assets';
        if ($path) {
            $static_base_path .= $path;
        }
        return $static_base_path;
    }

    模板里使用的時候就是<?php echo static_base_path();?>/images/cai1.png,或者直播<?php echo static_base_path('/images/cai1.png');?>

  • siwei 2024-02-18

    只是想能夠正確調(diào)用而已,如圖http://www.wtbis.cn/upload/img/20240218/1865d1ce1301b9.png 調(diào)用結(jié)果不能正確顯示。如果不用<?php echo static_base_path();?>/images/cai1.png ,如何寫才能正確調(diào)用。

  • siwei 2024-02-18

    app/view/assets/images/cai1.png 不能正確顯示

  • Madman 2024-02-18

    你直接打開 http://127.0.0.1:8787/app/view/assets/images/cai1.png 能打開圖片嗎? 如果不能打開,就在PublicController控制器里打印一些信息看看路由有沒有定位成功。

six

手冊說靜態(tài)文件不能使用 /app/ 開頭的路徑,因為以/app/開頭代表訪問某插件,也就是默認(rèn)訪問不到 public/app/ 目錄下的靜態(tài)文件。
例如 /app/view/assets/images/cai1.png 實際訪問的是 /plugin/view/public/app/view/assets/images/cai1.png
前綴不用/app/就好了
比如 /apps/view/assets/images/cai1.png 實際訪問的是 /public/apps/view/assets/images/cai1.png

靜態(tài)文件相關(guān)見手冊 http://www.wtbis.cn/doc/webman/static.html

  • 暫無評論
年代過于久遠(yuǎn),無法發(fā)表回答
??