您的位置:首页 > 编程语言 > PHP开发

Yii2 模块module笔记

2015-10-05 10:43 603 查看
包含内容:

使用GII新建module

建立子模块

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

1. 使用Gii工具新建module



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

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

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


3. 访问
http://你的项目后台地址/article/default/index
4. 调用模块中的操作

在后台某个控制器文件,如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下面的配置文件里
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: