Home  >  Article  >  PHP Framework  >  If you don’t study the source code of ThinkPHP, how will you know the role of the env file?

If you don’t study the source code of ThinkPHP, how will you know the role of the env file?

咔咔
咔咔Original
2020-10-09 15:44:351753browse

This article mainly introduces the framework's use of env files to load other types of configuration files. A previous article introduced the factory mode loading of configuration files.

9. How to let the framework load other types of configuration files using env files

When initializing the application There is an attribute in the module called configExt. This attribute is the extension of the file.

If you don’t study the source code of ThinkPHP, how will you know the role of the env file? Find where this attribute is set.

According to the config configuration file loading process, you can clearly see that the upper layer of the init method is the initialization application, which is the initialize method.

Then this attribute must have been set in advance before the init method.

Return to the previous level of the init method initialize and you can directly see the setting of this value.

If you don’t study the source code of ThinkPHP, how will you know the role of the env file?
Insert image description here

This value is obtained from the environment variable of env. If not, it defaults to php, so you need to create an env file. .

And give a default value of yaml

If you don’t study the source code of ThinkPHP, how will you know the role of the env file? Let me show you the changes. There is a bug in the 5.1.34 LTS version. Kaka is currently using 5.1.39 LTS This issue has been fixed.

I believe you can all see this code. It first sets the default value for configExt, and then loads the environment variable configuration file.

Then the code to load this environment variable is meaningless. The value of configExt will always be .php

Even if the configuration is made in the env file, it will not be obtained.

If you don’t study the source code of ThinkPHP, how will you know the role of the env file?A yaml configuration file was added to the config configuration file before

If you don’t study the source code of ThinkPHP, how will you know the role of the env file? Then at this time, you can use the config class to obtain the configuration of the yaml type file.

There is no problem after testing, the data can come out.

But we don’t dare to do this in actual projects! If you want to configure the value configExt to the environment variable, you need to convert all configuration files in the config directory to the corresponding type.

For example, if configExt is set to .ini, then all files in the config directory need to be changed to ini file format

If you don’t study the source code of ThinkPHP, how will you know the role of the env file?If you don’t study the source code of ThinkPHP, how will you know the role of the env file?This content is only for the application after reading the source code. Do not use it in actual projects because all configuration files in the tp framework are of PHP type.

If you change it to other types, you will need to modify all the configuration files in the framework. It would be a shame if you can do this!

Persistence in learning, persistence in blogging, and persistence in sharing are the beliefs that Kaka has always adhered to since its beginning. I hope that Kaka’s articles on the huge Internet can bring you a little bit of help. I’m Kaka, see you next time.

The above is the detailed content of If you don’t study the source code of ThinkPHP, how will you know the role of the env file?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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