yii2笔记: 模块(module)

官方文档

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

  

转载于:https://www.cnblogs.com/zergling9999/p/6081833.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值