Home > Article > PHP Framework > How thinkphp5 reads configuration files
When using the ThinkPHP5 framework, we usually need to read some configuration information in the application, such as database connection information, cache information, etc. In the ThinkPHP5 framework, all configuration files are stored in the config directory.
Naming of configuration files
In the ThinkPHP5 framework, the config directory saves all configuration files, and the file extensions of these files are. php. Name the configuration file according to the format of application configuration-controller configuration-method configuration.php, for example:
Application configuration file name: app.php
Controller configuration file name: index.php
Method configuration file name: hello.php
In the application, we You can read the application configuration file through the following code:
<code><pre class="brush:php;toolbar:false">$app_config = config(&#39;app&#39;);</pre>
Read the application configuration file
In the application, if you need to read the configuration file, you can use the config function and take the configuration file name as its parameter. The following code can be used to read the contents of the app.php configuration file
<code><pre class="brush:php;toolbar:false">$app_config = config(&#39;app&#39;);</pre>
The sample code for reading the application configuration file is as follows :
Read controller configuration file
The controller configuration file can be read in the controller through $this->config. The name of the controller configuration file is: controllername.php, such as:
class Index extends Controller { public function index() { $controller_config = $this->config; dump($controller_config); } }The sample code for reading the controller configuration file is as follows:
Reading method Configuration file
In the method, we can read the method configuration file through the config function. The name of the method configuration file is: method name.php, such as:
class Index extends Controller { public function hello() { $action_config = config('hello'); dump($action_config); } }The sample code for reading the method configuration file is as follows:
Read other files
In addition to app.php, controller configuration files and method configuration files, we can also read custom configuration files through the config function. The configuration file must be stored in the config directory and use .php as the file extension. The sample code for reading a custom configuration file is as follows:
<?php namespace app\index\controller; use think\Controller; class Index extends Controller { public function index() { $custom_config = config('custom'); dump($custom_config); } }The above is the detailed content of How thinkphp5 reads configuration files. For more information, please follow other related articles on the PHP Chinese website!