Configuration file

The EasySwoole framework provides a very flexible and free global configuration function. The configuration file is defined by PHP return array. For some simple applications, no configuration needs to be modified. For complex requirements, you can also extend your own independent configuration files and dynamically configure. . After the framework is installed, the default global configuration file is the produce.php, dev.php file in the project root directory (before version 3.1.2 is dev.env, produce.env) The file contents are as follows:


      return [
          'SERVER_NAME'   => "EasySwoole",//Service Name
          'MAIN_SERVER'   => [
              'LISTEN_ADDRESS' => '',//Listening address
              'PORT'           => 9501,//Listening port
              'SOCK_TYPE'      => SWOOLE_TCP,//This configuration item is valid when the SERVER_TYPE value is TYPE_SERVER.
              'RUN_MODEL'      => SWOOLE_PROCESS,// Default Server operating mode
              'SETTING'        => [// Run configuration of Swoole Server (full configuration visible [Swoole documentation] (
                  'worker_num'       => 8, // Number of worker processes running
                  'reload_async' => true, // Set the asynchronous restart switch. When set to true, the asynchronous secure restart feature is enabled and the worker process waits for the asynchronous event to complete before exiting.
                  'task_enable_coroutine' => true, //Automatically create coroutines in the onTask callback after opening
          'TEMP_DIR'      => null,//Temporary file storage directory
          'LOG_DIR'       => null,//Directory where log files are stored

EASYSWOOLE_SERVER, EASYSWOOLE_WEB_SOCKET_SERVER type, you need to set the callback (receive or message) in EasySwooleEvent.php mainServerCreate, otherwise it will be wrong

Configuration operation class

The configuration operation class is EasySwoole\Config, which is very simple to use. See the code example below. The operation class also provides the toArray method to get all the configuration. The load method overrides all configurations. Based on these two methods, you can Customize more advanced operations yourself

Setting and getting configuration items support dot syntax separation. See the code example for getting the configuration below.


$instance = \EasySwoole\EasySwoole\Config::getInstance();

// Get configuration separated by level with level

// Set configuration by level with a dot
$instance->setConf('', 'localhost');

// Get all configurations
$conf = $instance->getConf();

// Overwrite the current configuration item with an array
$conf['DATABASE'] = [
    'host' => '',
    'port' => 13306

After the server is started, the newly added configuration items are valid only for the process that performs the operation. If you need to share the configuration globally, you need to expand the configuration.

Add a user profile

Each application has its own configuration items. Adding your own configuration items is very simple. One of the methods is to add them directly to the configuration file, as in the following example.

/*################ MYSQL CONFIG ##################*/

'MYSQL' => [
    'host'          => '',
    'port'          => '3306',
    'user'          => 'root',
    'timeout'       => '5',
    'charset'       => 'utf8mb4',
    'password'      => 'root',
    'database'      => 'cry',
    'POOL_MAX_NUM'  => '20',
    'POOL_TIME_OUT' => '0.1',
/*################ REDIS CONFIG ##################*/
'REDIS' => [
    'host'          => '',
    'port'          => '6379',
    'auth'          => '',
    'POOL_MAX_NUM'  => '20',
    'POOL_MIN_NUM'  => '5',
    'POOL_TIME_OUT' => '0.1',

Production and development configuration separation

Under the php easyswoole start command, the default is development mode, loading dev.php (previous to 3.1.2) When running the php easyswoole start produce command, loadproduce.phpfor production mode (previouslyproduce.env` before 3.1.2)

DI injection configuration

Es3.x provides several Di parameter configurations, custom configuration script error exception handling callbacks, controller namespace, maximum resolution level, etc.

Di::getInstance()->set(SysConst::ERROR_HANDLER,function (){});//Configuration error handling callback
Di::getInstance()->set(SysConst::SHUTDOWN_FUNCTION,function (){});//Configuration script end callback
Di::getInstance()->set(SysConst::HTTP_CONTROLLER_NAMESPACE,'App\\HttpController\\');//Configuring the controller namespace
Di::getInstance()->set(SysConst::HTTP_CONTROLLER_MAX_DEPTH,5);//Configure the maximum resolution level of the http controller.
Di::getInstance()->set(SysConst::HTTP_EXCEPTION_HANDLER,function (){});//Configure http controller exception callback
Di::getInstance()->set(SysConst::HTTP_CONTROLLER_POOL_MAX_NUM,15);//The maximum number of http controller object pools

Dynamic configuration

After the 3.2.5 version of EasySwoole, the default config storage driver was changed to swoole_table. As long as the configuration is modified, other processes are also effective.

Config driver

After EasySwoole is released in version 3.2.5, the default configuration of the driver storage is changed from SplArray to swoole_table. After the configuration is modified, all processes take effect at the same time.


The AbstractConfig abstract class provides the following methods for driving inheritance to other config drivers.

  • __construct(bool $isDev = true) Pass in the parameters of the development environment, according to the parameter to load dev.php or produce.php
  • isDev() This method can be used to obtain whether the current operating environment is a development environment.
  • abstract function getConf($key = null); Get a configuration
  • abstract function setConf($key,$val):bool ; Set a parameter
  • abstract function load(array $array):bool ; Reload configuration item
  • abstract function merge(array $array):bool ; Merge configuration item
  • abstract function clear():bool ; Clear all configuration items

Custom configuration

In EasySwoole, it comes with SplArray and swoole_table driver implementation, you can check the source code to understand.

The default driver is swoole_table

If you need to modify the storage driver, the steps are as follows:

  • Inherit AbstractConfig to implement each method
  • <?php
    public static function initialize()
    //Obtain the original config configuration item and load it into the new configuration item.
    $config = Config::getInstance()->getConf();
    Config::getInstance()->storageHandler(new SplArrayConfig())->load($config);
    // TODO: Implement initialize() method.

Dynamic configuration problem

Since swoole is multi-process, if you use SplArray to store, after a single process modifies the configuration, other processes will not take effect. If you use the swoole_table method, all of them will take effect.


  • QQ exchange group

    • VIP group 579434607 (this group needs to pay 599 RMB)
    • EasySwoole official group 633921431 (full)
    • EasySwoole official two groups 709134628 (full)
    • EasySwoole official three groups 932625047 (full)
    • EasySwoole official four groups 779897753 (full)
    • EasySwoole official five groups 853946743
  • Business support:

    • QQ 291323003
    • EMAIL
  • Author WeChat

  • Donation Your donation is the greatest encouragement and support for the Swoole project development team. We will insist on development and maintenance. Your donation will be used to:

    • Continuous and in-depth development
    • Document and community construction and maintenance
  • easySwoole's documentation uses GitBook as a document writing tool. If you find that the document needs to be corrected/supplemented during use, please fork project's document repository, modify and supplement it. Submit Pull Request and contact us