YII框架的模块化技术

一、模块的创建

利用yii的自动生成工具gii生成模块。

1、访问:lcoalhost/web/index.php?r=gii

2、点击 Module Generator 下面的 start

3、填写 Module Class 例如:app\modules\article\Article

解释:app\modules\模块名称\模块入口名称

4、填写 Module ID 例如:Article

解释:模块唯一id,一般与模块入口名称相同。

5、点击 Preview

6、点击 Generate 后得到如下代码

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

将它写入配置文件。

自动生成的代码,在网站根目录下的modules里面。

二、模块的使用

1、被其它模块调用

<?php
namespace app\controllers; 
use yii\web\Controller; 
use \YII; 
class TestController extends Controller 
{
    public function actionIndex() { 
        //获取子模块
        $article = \YII::$app->getModule('article'); //参数是模块的id
        //调用子模块的操作
        $article->runAction('default/index'); //参数是 控制器/操作
    }
}

2、直接访问模块

例如:http://www.testyii.com/web/index.php?r=article/default/index

解释:localhost/web/index.php?r=模块ID/控制器名/操作名

posted @ 2015-10-05 21:01  扬空  阅读(326)  评论(0编辑  收藏  举报