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/"
__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/"
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/"
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:
Open config. php configuration file, find the following code:
<code>return array(<br/> // ...省略其他配置代码<br/> // 项目相关配置<br/> 'TMPL_PARSE_STRING' =>array(<br/> '__PUBLIC__' => '/public',// 更改默认的/Public 替换规则<br/> '__ROOT__' => '', // 更改默认的/ 替换规则<br/> '__APP__' => '/Application/', // 更改默认的/App 替换规则<br/> ),<br/> // ...省略其他配置代码<br/>);<br/></code>
<code>return array(<br/> // ...省略其他配置代码<br/> // 项目相关配置<br/> 'TMPL_PARSE_STRING' =>array(<br/> '__PUBLIC__' => '/static',// 将/Public替换为/static<br/> '__ROOT__' => '', // 根目录不变<br/> '__APP__' => '/app/', // 将/App替换为/app/<br/> ),<br/> // ...省略其他配置代码<br/>);<br/></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__ . '/css/style.css'; ?>
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!