Home > PHP Framework > ThinkPHP > body text

TP5.1 Predefined environment variables

藏色散人
Release: 2020-03-02 14:07:26
forward
4249 people have browsed it

In TP5.1, some of the predefined constants of previous versions such as APP_PATH, ROOT_PATH, etc. are defined as project environment variables.

The current predefined environment variables include (with the project path E:\wamp64\ www\project_name for example):

think_path: System framework directory path, such as E:\wamp64\www\project_name \thinkphp\

root_path: Project path, such as E:\wamp64\www\project_name

app_path: Application path, such as E:\wamp64\www\project_name\application\

config_path: Configuration file directory path, such as: E:\wamp64\www\project_name\config\

route_path: Routing file path: such as: E:\wamp64\ www\project_name\route\

runtime_path: Runtime directory path: For example: E:\wamp64\www\project_name\runtime\

extend_path: Extended class library path, such as: E:\wamp64\www\project_name\extend\

vendor_path: Third-party class library directory path, such as: E:\wamp64\www\ project_name\vendor\

module_path: The path of the current module

Manage environment variables

think\Env object is responsible for the project environment Access to variables.

Set environment variables

Set project environment variables through the set method of the Env object.

Env::set('name', 'value');
Copy after login

Read the environment Variables

You can quickly obtain predefined environment variables through the helper function env(). This function finally calls the get method in the Env object.

env('name'); // 参数为空时, 获取所有的项目环境变量
Env::get('name');
Copy after login

To obtain environment variables, it will be obtained first If the predefined project environment variables are not found, the PHP environment variables will be obtained. If none are found, null will be returned. However, when obtaining the PHP built-in environment variables, the TP framework will append the PHP_ prefix to the variable name by default, which needs to be cancelled. Automatically append variable name prefix:

env('document_root', null, false); // 第三个参数指定为false, 获取$_SERVER['DOCUMENT_ROOT']
Copy after login

Application: Configure a common template layout file for each module

Configure in template.php:

'layout_name' => env('app_path') . 'common/view/layout.html', // 以绝对路径的方式指定通用的布局文件
Copy after login

Then create the layout.html layout file in the view directory under the common directory.

Recommended: "thinkPHP Tutorial"

The above is the detailed content of TP5.1 Predefined environment variables. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:cnblogs.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 [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!