Yii2 模块module笔记

包含内容:

使用GII新建module

建立子模块

在其他控制器中调用模块的操作(action)

1. 使用Gii工具新建module

注意模块的路径,我们没有写backendmodulesArticle。多了一层article目录是为了防止如果有多个模块共用同一文件。

2. 在backendconfigmain.php中添加配置代码。

  'modules' => [
        'article' => [
            'class' => 'backendmodulesarticleArticle',
        ],
    ],

3. 访问

http://你的项目后台地址/article/default/index

4. 调用模块中的操作

在后台某个控制器文件,如backendcontrollersCarController.php 中添加调用模块动作的代码

    public function actionIndex()
    {

        // 获取子模块
        $arcileModule = Yii::$app->getModule('article');
        // 调用子模块操作
        $arcileModule->runAction('default/index');
        ........

5. 建立子模块。在article下新建留言comment模块

Module Class填写:backendmodulesarticlemodulescommentComment

6. 添加配置信息

打开backendmodulesarticleArticle.php。在init方法内加入

    public function init()
    {
        parent::init();

        $this->modules = [  
            'comment' => [
                'class' => 'backendmodulesarticlemodulescommentComment',
            ],
        ];
        // custom initialization code goes here
    }

7. 访问

http://你的项目后台地址/article/comment/default/index

若要直接访问,http://你的项目后台地址/comment/default/index

需要将刚才的配置信息加入到backendconfigmain.php下面的配置文件里

原文地址:https://www.cnblogs.com/mafeifan/p/4855437.html