【问题标题】:Yii2 set config file for moduleYii2 为模块设置配置文件
【发布时间】:2014-11-19 22:51:15
【问题描述】:

如何在 Yii2 中创建我的配置?并返回例如 $新 = '新' 然后在其他类中使用它? 我的结构

 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
    }
  }

【问题讨论】:

  • 你可以看看我自己的question中的链接

标签: php structure yii2


【解决方案1】:

在 Module 类中设置配置文件,如下所示:

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'];

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-19
      • 2016-05-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-26
      相关资源
      最近更新 更多