thinkphp5 加载静态资源路径与常量的方法

2018-11-0513:37:27后端程序开发Comments2,263 views字数 1328阅读模式

1、加载静态资源路径文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/7667.html

大于5.0.4版本可以直接使用文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/7667.html

__ROOT__ :项目目录文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/7667.html

__STATIC__ :项目目录下的static目录文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/7667.html

__JS__ :项目目录下的static/js目录文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/7667.html

__css__:项目目录下的static/css目录文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/7667.html

我们可以使用view模板打印这些常量的具体路径文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/7667.html

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Insert title here</title>
  6. </head>
  7. <body>
  8. __ROOT__<br>
  9. __STATIC__<br>
  10. __JS__<br>
  11. __CSS__<br>
  12. </body>
  13. </html>

页面输出如下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/7667.html

  1. /
  2. /projectname/public/static
  3. /projectname/public/static/js
  4. /projectname/public/static/css

打开application文件夹下的config文件,可根据你自己需要自定义资源常量,定义好后常量就可以在模板文件中使用常量了。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/7667.html

例如:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/7667.html

  1. <?php
  2. return [
  3.  'view_replace_str' => [
  4.   '__PUBLIC__'=>'../public/static/admin',
  5.   '__ROOT__' => '/',
  6.   '__APP__' => 'app/admin/',
  7.  ]
  8. ];

2、预定义常量文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/7667.html

预定义常量是指系统内置定义好的常量,不会随着环境的变化而变化,包括:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/7667.html

EXT           类库文件后缀(.php)文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/7667.html

THINK_VERSION 框架版本号文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/7667.html

3、路径常量文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/7667.html

系统和应用的路径常量用于系统默认的目录规范,可以通过重新定义改变,如果不希望定制目录,这些常量一般不需要更改。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/7667.html

DS 当前系统的目录分隔符文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/7667.html

THINK_PATH 框架系统目录文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/7667.html

ROOT_PATH 框架应用根目录文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/7667.html

APP_PATH 应用目录(默认为application)文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/7667.html

CONF_PATH 配置目录(默认为APP_PATH)文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/7667.html

LIB_PATH 系统类库目录(默认为 THINK_PATH.'library/')文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/7667.html

CORE_PATH 系统核心类库目录 (默认为 LIB_PATH.'think/')文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/7667.html

TRAIT_PATH 系统trait目录(默认为 LIB_PATH.'traits/')文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/7667.html

EXTEND_PATH 扩展类库目录(默认为 ROOT_PATH . 'extend/')文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/7667.html

VENDOR_PATH 第三方类库目录(默认为 ROOT_PATH . 'vendor/')文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/7667.html

RUNTIME_PATH 应用运行时目录(默认为 ROOT_PATH.'runtime/')文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/7667.html

LOG_PATH 应用日志目录 (默认为 RUNTIME_PATH.'log/')文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/7667.html

CACHE_PATH 项目模板缓存目录(默认为 RUNTIME_PATH.'cache/')文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/7667.html

TEMP_PATH 应用缓存目录(默认为 RUNTIME_PATH.'temp/')文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/7667.html

4、系统常量文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/7667.html

系统常量会随着开发环境的改变或者设置的改变而产生变化。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/7667.html

IS_WIN 是否属于Windows 环境文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/7667.html

IS_CLI 是否属于命令行模式文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/7667.html

THINK_START_TIME 开始运行时间(时间戳)文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/7667.html

THINK_START_MEM 开始运行时候的内存占用文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/7667.html

ENV_PREFIX 环境变量配置前缀文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/7667.html

  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/bc/7667.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定