Yii2 设置模块的配置文件


Yii2 set config file for module

如何在 Yii2 中创建我的配置?并返回其中$new = "新"然后在其他课程中使用它?我的结构

 Module
    MyModule
      config (directory)
        config.php (here must be $new = 'new')
      function (directory)
        MyFunc ( here i need use variable from config)

感谢您的帮助!代码来自

    <?php
    namespace module'MyModule'function;
    class MyFunc
  {
   private static function func()
    {
   here i need to get $new from config
    }
  }

模块类中设置配置文件,如下所示:

class Module extends 'yii'base'Module    {
    public function init(){
        parent::init();
        'Yii::configure($this, require __DIR__ . '/config.php');
}

你的配置文件是这样的:

return [
   'params' => [ 
       'test' => 'this is the test params'
   ],
   'components' => [
     // ...
   ],
];

访问模块中的参数:

'Yii::$app->getModule('MyModule')->params['test'];

为什么要在模块里面配置文件?将配置文件放在通用配置目录中,此配置将在您的应用程序中可用。