Configuration and acquisition of thinkphp5.1 config

Time:2020-7-31

First of all, the Config class needs to be introduced into the controller. Here, the new facade in 5.1 is used. Through the facade, you can statically call methods that need to be inherited before they can be used.

Get configuration:

namespace app\index\controller;
use think\facade\Config;
 
class index
{
  public function index()
  {
    //Get all the configuration contents and return an array
    dump(Config::get());
    //Get the configuration content in the app and return an array
    dump(Config::get('app.'));
    //Get the configuration content in the app and return an array
    dump(Config::pull('app'));
    //Get the configuration content in debug in app
    dump(Config::get('app.app_debug'));
  }
}
 
//App is the default primary configuration item, app_ Debug is under the app configuration item, so app. Can be omitted, but
//Like template.type You can't omit the template. Once omitted, it will become the default type under the app

Before getting the configuration, it is better to judge whether the configuration exists

namespace app\index\controller;
use think\facade\Config;
 
class index
{
  public function index()
  {
    //Determine whether the type item under template exists, and return true or false
    dump(Config::has('template.type'));
  }
}

Dynamic settings configuration


namespace app\index\controller;
use think\facade\Config;
public function set()
{
  dump(Config::get('app_debug'));
  Config::set('app_debug',false);
  dump(Config::get('app_debug'));
}

helper function

public function helper()
  {
    //Get configuration
    dump(config('database.hostname'));
    //Use '?' to determine whether the configuration exists
    dump(config('?database.hostname'));
    //Setup configuration
    config('database.hostname','localhost');
    //Get configuration
    dump(config('database.hostname'));
 
    //It is recommended to use the method of static class config:: to get or set the configuration
    //More easily supported by IDE
    //Any helper function does not depend on the class passed in, such as the config helper function
    //Does not rely on the Config class
  }
}

This article on the configuration of thinkphp5.1 config and get detailed explanation of the article introduced here, more relevant thinkphp5.1 config configuration content, please search the previous articles of developeppaer or continue to browse the related articles below, I hope you can support developeppaer more in the future!