如何在Yii2中创建配置?并以它为例回报 $ new ='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
}
}
答案 0 :(得分:0)
在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'];
答案 1 :(得分:-1)
为什么要在模块里面配置文件?将configFile放在公共配置目录中,此配置将在您的应用程序中实现。