https://www.edoou.com/articles/1556848583530922
ThinkPHP5.1 里面__PUBLIC__無法生效的問題
在用PHP
模板的時候需要引用外部的樣式文件,之前的版本直接用__PUBLIC__
就可以定位到指定的位置。
<Link href="__PUBLIC__/static/css/main.css" rel="stylesheet" />
但是頁面中__PUBLIC__
並沒有解析成對應的路徑。
在查詢TP5.1的文檔時候,有這么一句話。“view_replace_str配置參數改成template配置文件的tpl_replace_string配置參數。”所以需要在config/template.php
中設置tpl_replace_string
的值。
我們直接添加這條配置項,代碼如下。
'tpl_replace_string' =>[ '__PUBLIC__' => $_SERVER['REQUEST_SCHEME']."://".$_SERVER['HTTP_HOST'].rtrim(dirname($_SERVER['SCRIPT_NAME'])), ],
你也可以自己設置成固定樣式。
'tpl_replace_string' => ['__PUBLIC__'=>'/項目名/public'],
這時候刷新頁面看一下,發現還是__PUBLIC__
並沒有轉義。這里是由於Runtime
下面有緩存文件。把Runtime
下的文件都刪除一下,就可以了。
一.index.php 入口文件加入
define('SCRIPT_DIR', rtrim(dirname($_SERVER['SCRIPT_NAME']), '\/\\'));
thinkphp5.1以下版本設置__STATIC__ __JS__ __CSS__等常量thinkphp在think目錄的view.php
$baseReplace = [ '__ROOT__' => $root, '__URL__' => $base . '/' . $request->module() . '/' . Loader::parseName($request->controller()), '__STATIC__' => $root . '/static', '__CSS__' => $root . '/static/css', '__JS__' => $root . '/static/js', ];
可以在config中修改和重新定義
// 視圖輸出字符串內容替換 'view_replace_str' => [ '__IMAGE__' => '/static/images', '__UPLOAD__' =>'/upload', ],
thinkphp5.1版本設置
在config目錄的template.php加入
'tpl_replace_string' =>[ '__STATIC__'=> SCRIPT_DIR . '/static', //后台程序css,img,js所在文件 '__COMMON__'=> SCRIPT_DIR . '/common', //前后共有css,img,js所在文件 '__APP__' => SCRIPT_DIR . '/', //定義首頁 '__JS__'=> SCRIPT_DIR . '/static/js', //js文件 '__CSS__'=> SCRIPT_DIR . '/static/css', //css文件 '__IMAGE__'=> SCRIPT_DIR . '/static/images', //image文件 ],