Yii2 模块module笔记

包含内容:php

使用GII新建moduleapp

创建子模块工具

在其余控制器中调用模块的操做(action)this

 

1. 使用Gii工具新建modulespa

 

注意模块的路径,咱们没有写backend\modules\Article。多了一层article目录是为了防止若是有多个模块共用同一文件。code

 

2. 在backend\config\main.php中添加配置代码。blog

  'modules' => [
        'article' => [
            'class' => 'backend\modules\article\Article',
        ],
    ],

 

3. 访问ci

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

 

4. 调用模块中的操做it

在后台某个控制器文件,如backend\controllers\CarController.php 中添加调用模块动做的代码

    public function actionIndex()
    {

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

 

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

Module Class填写:backend\modules\article\modules\comment\Comment

 

6. 添加配置信息

打开backend\modules\article\Article.php。在init方法内加入

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

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

 

7. 访问

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

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

须要将刚才的配置信息加入到backend\config\main.php下面的配置文件里

相关文章
相关标签/搜索