Home > PHP Framework > ThinkPHP > body text

How to implement path replacement in thinkphp

PHPz
Release: 2023-05-29 12:31:46
forward
1286 people have browsed it

What is path replacement

Using specific variables to replace paths is to make it more convenient and flexible during thinkphp development and debugging. Path replacement in thinkphp is mainly implemented through three variables, namely __APP__, __PUBLIC__ and __ROOT__.

The variable __APP__ represents the directory containing the index.php file, which is the root directory of the application. Taking the default settings as an example, using __APP__ instead of the path can generate the following path:

"/home/wwwroot/default/ThinkPHP/Application/"
Copy after login

__PUBLIC__ The global variable refers to the path of the public directory, that is, it is deployed to the public directory under the root directory of the website. For example, you can use the following code to get the path to the public directory:

"/home/wwwroot/default/test/public/"
Copy after login

Indicates the directory where the entire application is located is the __ROOT__ variable, which represents the root directory of the application. Here's one way to rewrite it: By default, using "__ROOT__" alternative path can achieve path simplification

"/home/wwwroot/default/ThinkPHP/"
Copy after login

How to perform path replacement

In thinkphp, you can use the configuration file or in the code Use variables directly for path replacement.

Use configuration files for path replacement
In thinkphp, you can edit the config.php configuration file to perform path replacement. The specific steps are as follows:

  1. Open config. php configuration file, find the following code:


    <code>return array(<br/>  // ...省略其他配置代码<br/>  // 项目相关配置<br/>  &#39;TMPL_PARSE_STRING&#39;  =>array(<br/>   &#39;__PUBLIC__&#39; => &#39;/public&#39;,// 更改默认的/Public 替换规则<br/>   &#39;__ROOT__&#39; => &#39;&#39;, // 更改默认的/ 替换规则<br/>   &#39;__APP__&#39; => &#39;/Application/&#39;, // 更改默认的/App 替换规则<br/>  ),<br/>  // ...省略其他配置代码<br/>);<br/></code>
    Copy after login


  2. ##Modify the corresponding values ​​​​of __PUBLIC__, __ROOT__, __APP__ , for example, change it to the following code:


    <code>return array(<br/>  // ...省略其他配置代码<br/>  // 项目相关配置<br/>  &#39;TMPL_PARSE_STRING&#39;  =>array(<br/>   &#39;__PUBLIC__&#39; => &#39;/static&#39;,// 将/Public替换为/static<br/>   &#39;__ROOT__&#39; => &#39;&#39;, // 根目录不变<br/>   &#39;__APP__&#39; => &#39;/app/&#39;, // 将/App替换为/app/<br/>  ),<br/>  // ...省略其他配置代码<br/>);<br/></code>
    Copy after login


  3. ##Save the changes and use __PUBLIC__, __ROOT__ in the code , __APP__ performs path replacement.
Perform path replacement directly in the code

Using variables in the code to perform path replacement is another feasible method, which is not limited to Used for path replacement in configuration files. Taking __PUBLIC__ as an example, you can use the following code for path replacement:

<?php
  // 在php文件中,可以使用变量__PUBLIC__来代替public目录的路径
  require __PUBLIC__ . &#39;/css/style.css&#39;;
 ?>
Copy after login

The above is the detailed content of How to implement path replacement in thinkphp. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template