模块配置文件

问题描述 投票:0回答:1

我是我的api模块的module.php文件,我添加了这个

/**
 * {@inheritdoc}
 */
public function init()
{
    parent::init();

     // custom initialization code goes here
     \Yii::configure($this, require __DIR__ . '/config/main.php');
}

在我的控制器中我有这个

public function actionIndex()
{
   dd(Yii::$app->getModule('payment')->params['data']);

}

在我的模块`main.php中我有这个

$params = ['data' => [ ... ]];

    if (YII_ENV == 'dev') {


if (YII_ENV == 'dev') {
    $params = array_merge(
        require __DIR__ . '/../../../../common/config/params.php',
        require __DIR__ . '/../../../../common/config/params-local.php',
        require __DIR__ . '/params.php',
    );
} else {
    $params = array_merge(
        require __DIR__ . '/../../../../common/config/params.php',
        require __DIR__ . '/params.php',
    );
}

return [
    'params' => $params,
]; 

我收到此错误

Undefined array key "data"
任何人都知道我在这里缺少什么,我正在尝试创建一个模块配置。谢谢你。

php yii2
1个回答
0
投票

模块中没有main.php(app/config中有)。 在每个模块中,modules/your_module 脚手架中都有一个 Module.php 类

如果你需要一个模块的特定 $params 数组,你应该声明 params var 并在声明或 init() 函数中分配您的值

class Module extends \yii\base\Module
{
    public $params = [];

   .....
   ......

 /**
 * {@inheritdoc}
 */
public function init()
{
    parent::init();

     // custom initialization code goes here
     $params['data'] = [......]; 
     
}
© www.soinside.com 2019 - 2024. All rights reserved.