Home  >  Article  >  PHP Framework  >  About the constants used in ThinkPhp view path __STATIC__ __JS__ __CSS__, etc.

About the constants used in ThinkPhp view path __STATIC__ __JS__ __CSS__, etc.

藏色散人
藏色散人forward
2020-09-04 13:42:403207browse

下面由thinkphp框架教程栏目给大家介绍ThinkPhp view路径用到的常量 __STATIC__ __JS__ __CSS__等,希望对需要的朋友有所帮助!

About the constants used in ThinkPhp view path __STATIC__ __JS__ __CSS__, etc.

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的值。

我们直接添加这条配置项,代码如下。

&#39;tpl_replace_string&#39; =>[    
    &#39;__PUBLIC__&#39; => $_SERVER[&#39;REQUEST_SCHEME&#39;]."://".$_SERVER[&#39;HTTP_HOST&#39;].rtrim(dirname($_SERVER[&#39;SCRIPT_NAME&#39;])),],

你也可以自己设置成固定样式。

&#39;tpl_replace_string&#39; => [&#39;__PUBLIC__&#39;=>&#39;/项目名/public&#39;],

这时候刷新页面看一下,发现还是__PUBLIC__并没有转义。这里是由于Runtime下面有缓存文件。把Runtime下的文件都删除一下,就可以了。

一.index.php 入口文件加入

define(&#39;SCRIPT_DIR&#39;, rtrim(dirname($_SERVER[&#39;SCRIPT_NAME&#39;]), &#39;\/\\&#39;));

 thinkphp5.1以下版本设置__STATIC__ __JS__ __CSS__等常量thinkphp在think目录的view.php

  $baseReplace = [            &#39;__ROOT__&#39;   => $root,
            &#39;__URL__&#39;    => $base . &#39;/&#39; . $request->module() . &#39;/&#39; . Loader::parseName($request->controller()),
            &#39;__STATIC__&#39; => $root . &#39;/static&#39;,
            &#39;__CSS__&#39;    => $root . &#39;/static/css&#39;,
            &#39;__JS__&#39;     => $root . &#39;/static/js&#39;,
        ];

可以在config中修改和重新定义

 // 视图输出字符串内容替换
    'view_replace_str' => [        '__IMAGE__'     => '/static/images',
        '__UPLOAD__'     =>'/upload',

    ],

thinkphp5.1版本设置

在config目录的template.php加入

   &#39;tpl_replace_string&#39; =>[        &#39;__STATIC__&#39;=> SCRIPT_DIR . &#39;/static&#39;,   //后台程序css,img,js所在文件
        &#39;__COMMON__&#39;=> SCRIPT_DIR . &#39;/common&#39;,   //前后共有css,img,js所在文件
        &#39;__APP__&#39;   => SCRIPT_DIR . &#39;/&#39;,               //定义首页
        &#39;__JS__&#39;=> SCRIPT_DIR . &#39;/static/js&#39;,  //js文件
        &#39;__CSS__&#39;=> SCRIPT_DIR . &#39;/static/css&#39;,  //css文件
        &#39;__IMAGE__&#39;=> SCRIPT_DIR . &#39;/static/images&#39;,  //image文件
    ],

The above is the detailed content of About the constants used in ThinkPhp view path __STATIC__ __JS__ __CSS__, etc.. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete