YII框架的模块化技术

来源:互联网 发布:南通农村商业银行软件 编辑:程序博客网 时间:2024/06/05 00:45

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、被其它模块调用

1
2
3
4
5
6
7
8
9
10
11
12
13
<?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/控制器名/操作名

0 0
原创粉丝点击