官方文档

http://www.yiiframework.com/doc-2.0/guide-structure-modules.html

 

自己的一些理解:

application和console本质上也都是module

我们建立的module实质上都是application的子模块

 

module里加载特定配置

Module.php

<?php

namespace app\modules\testmod;

/**
 * testmod module definition class
 */
class Module extends \yii\base\Module
{
    /**
     * @inheritdoc
     */
    public $controllerNamespace = 'app\modules\testmod\controllers';

    /**
     * @inheritdoc
     */
    public function init()
    {
        parent::init();
        \Yii::configure($this, require(__DIR__ . '/config.php'));

        // custom initialization code goes here
    }
}

  

config.php,这里和config/web.php的原理是一样的。

<?php
return [
    'components' => [
        'db' => require(__DIR__ . '/db.php'),
    ],
    'params' => [
        'adminEmail' => 'testmod@example.com',
    ],
];

  

控制器里使用模块的配置

 

        print \Yii::$app->params['adminEmail']; // application的参数
        print $this->module->params['adminEmail']; // 当前模块的参数
        print $this->module->db->createCommand("SELECT COUNT(1) FROM testmod")->queryScalar(); // 当前模块的组件

  

视图里使用模块的配置

 

<?= $this->context->module->params['adminEmail'] ?>

  

相关文章:

  • 2022-02-08
  • 2021-09-17
  • 2021-12-10
  • 2021-06-17
  • 2022-12-23
  • 2021-12-20
  • 2021-11-25
猜你喜欢
  • 2022-12-23
  • 2021-06-22
  • 2021-08-11
  • 2021-11-30
  • 2021-08-28
  • 2021-09-23
  • 2021-07-31
相关资源
相似解决方案