Home > PHP Framework > ThinkPHP > How thinkphp5 reads configuration files

How thinkphp5 reads configuration files

PHPz
Release: 2023-05-31 15:34:45
forward
1289 people have browsed it

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.

  1. 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><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>$app_config = config(&amp;#39;app&amp;#39;);</pre><div class="contentsignin">Copy after login</div></div><div class="contentsignin">Copy after login</div></div>

  1. 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><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>$app_config = config(&amp;#39;app&amp;#39;);</pre><div class="contentsignin">Copy after login</div></div><div class="contentsignin">Copy after login</div></div>

The sample code for reading the application configuration file is as follows :

Copy after login
  1. 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);
    }
}
Copy after login

The sample code for reading the controller configuration file is as follows:

Copy after login
  1. 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(&#39;hello&#39;);
        dump($action_config);
    }
}
Copy after login

The sample code for reading the method configuration file is as follows:

Copy after login
  1. 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(&#39;custom&#39;);
        dump($custom_config);
    }
}
Copy after login

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!

Related labels:
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 admin@php.cn
Latest Issues
Why thinkphp has better performance than laravel?
From 1970-01-01 08:00:00
0
0
0
ThinkPHP Why use composer?
From 1970-01-01 08:00:00
0
0
0
thinkphp versions supported by php6
From 1970-01-01 08:00:00
0
0
0
thinkphp upload files
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template