zoukankan      html  css  js  c++  java
  • ThinkPhp view 路径用到的常量 __STATIC__ __JS__ __CSS__等

    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文件
        ],
  • 相关阅读:
    Fiddler抓包
    用powershell Crescendo模块,把【linux字符命令】包装成【powershell对象命令】
    初探设计模式-单例模式
    dev的CheckedListBoxControl的使用
    git的安装及使用(三)----SSH连接
    go——杂碎小知识
    goland安装+打印hello world
    git的安装及使用(二)
    git的安装及使用(一)
    xxx
  • 原文地址:https://www.cnblogs.com/jiangfeilong/p/11214555.html
Copyright © 2011-2022 走看看