您好,欢迎访问一九零五行业门户网

关于ThinkPhp view路径用到的常量 __STATIC__ __JS__ __CSS__等

下面由thinkphp框架教程栏目给大家介绍thinkphp view路径用到的常量 __static__ __js__ __css__等,希望对需要的朋友有所帮助!
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文件 ],
以上就是关于thinkphp view路径用到的常量 __static__ __js__ __css__等的详细内容。
其它类似信息

推荐信息