Home >PHP Framework >ThinkPHP >TP5.1 Predefined environment variables
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');
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');
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']
Application: Configure a common template layout file for each module
Configure in template.php:
'layout_name' => env('app_path') . 'common/view/layout.html', // 以绝对路径的方式指定通用的布局文件
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!