- Yii 教程
 - Yii - 主页
 - Yii - 概述
 - Yii - 安装
 - Yii - 创建页面
 - Yii - 应用程序结构
 - Yii - 入口脚本
 - Yii - 控制器
 - Yii - 使用控制器
 - Yii - 使用动作
 - Yii - 模型
 - Yii - 小部件
 - Yii - 模块
 - Yii - 视图
 - Yii - 布局
 - Yii - 资产
 - Yii - 资产转换
 - Yii - 扩展
 - Yii - 创建扩展
 - Yii - HTTP 请求
 - Yii - 响应
 - Yii - URL 格式
 - Yii - URL 路由
 - Yii - URL 规则
 - Yii - HTML 表单
 - Yii - 验证
 - Yii - 临时验证
 - Yii - AJAX 验证
 - Yii - 会话
 - Yii - 使用闪存数据
 - Yii - cookie
 - Yii - 使用 Cookie
 - Yii - 文件上传
 - Yii - 格式化
 - Yii - 分页
 - Yii - 排序
 - Yii - 属性
 - Yii - 数据提供者
 - Yii - 数据小部件
 - Yii - 列表视图小部件
 - Yii - GridView 小部件
 - Yii - 活动
 - Yii - 创建事件
 - Yii - Behave
 - Yii - 创建Behave
 - Yii - 配置
 - Yii - 依赖注入
 - Yii - 数据库访问
 - Yii - 数据访问对象
 - Yii - 查询生成器
 - Yii - 活动记录
 - Yii - 数据库迁移
 - Yii - 主题化
 - Yii - RESTful API
 - Yii - RESTful API 的实际应用
 - Yii - 字段
 - Yii - 测试
 - Yii - 缓存
 - Yii - 片段缓存
 - Yii - 别名
 - Yii - 日志记录
 - Yii - 错误处理
 - Yii - 身份验证
 - Yii - 授权
 - Yii - 本地化
 - Yii-Gii
 - Gii – 创建模型
 - Gii – 生成控制器
 - Gii – 生成模块
 
- Yii 有用的资源
 - Yii - 快速指南
 - Yii - 有用的资源
 - Yii - 讨论
 
Yii - 模块
模块是具有自己的模型、视图、控制器和可能的其他模块的实体。它实际上是应用程序内的应用程序。
步骤 1 -在项目根目录中创建一个名为“modules”的文件夹。在 module 文件夹中,创建一个名为hello的文件夹。这将是我们的 Hello 模块的基本文件夹。
步骤 2 - 在hello文件夹内,使用以下代码创建文件Hello.php 。
<?php
   namespace app\modules\hello;
   class Hello extends \yii\base\Module {
      public function init() {
         parent::init();
      }
   }
?>
我们刚刚创建了一个模块类。它应该位于模块的基本路径下。每次访问模块时,都会创建相应模块类的实例。init ()函数用于初始化模块的属性。
步骤 3 - 现在,在 hello 文件夹中添加两个目录 - 控制器和视图。将CustomController.php文件添加到控制器的文件夹中。
<?php
   namespace app\modules\hello\controllers;
   use yii\web\Controller;
   class CustomController extends Controller {
      public function actionGreet() {
         return $this->render('greet');
      }
   }
?>
创建模块时,约定是将控制器类放入模块基本路径的控制器目录中。我们刚刚定义了actionGreet函数,它只返回一个问候视图。
模块中的视图应放置在模块基本路径的views 文件夹中。如果视图由控制器渲染,它们应该位于与controllerID对应的文件夹中。将自定义文件夹添加到视图文件夹。
步骤 4 - 在自定义目录中,使用以下代码创建一个名为greet.php的文件。
<h1>Hello world from custom module!</h1>
我们刚刚为我们的actionGreet创建了一个视图。要使用这个新创建的模块,我们应该配置应用程序。我们应该将模块添加到应用程序的 module 属性中。
步骤 5 - 修改config/web.php文件。
<?php
   $params = require(__DIR__ . '/params.php');
   $config = [
      'id' => 'basic',
      'basePath' => dirname(__DIR__),
      'bootstrap' => ['log'],
      'components' => [
         'request' => [
            // !!! insert a secret key in the following (if it is empty) - this is
               //required by cookie validation
            'cookieValidationKey' => 'ymoaYrebZHa8gURuolioHGlK8fLXCKjO',
         ],
         'cache' => [
            'class' => 'yii\caching\FileCache',
         ],
         'user' => [
            'identityClass' => 'app\models\User',
            'enableAutoLogin' => true,
         ],
         'errorHandler' => [
            'errorAction' => 'site/error',
         ],
         'mailer' => [
            'class' => 'yii\swiftmailer\Mailer',
            // send all mails to a file by default. You have to set
            // 'useFileTransport' to false and configure a transport
            // for the mailer to send real emails.
            'useFileTransport' => true,
         ],
         'log' => [
            'traceLevel' => YII_DEBUG ? 3 : 0,
            'targets' => [
               [
                  'class' => 'yii\log\FileTarget',
                  'levels' => ['error', 'warning'],
               ],
            ],
         ],
         'db' => require(__DIR__ . '/db.php'),
      ],
      'modules' => [
         'hello' => [
            'class' => 'app\modules\hello\Hello', 
         ],
      ],
      'params' => $params,
   ];
   if (YII_ENV_DEV) {
      // configuration adjustments for 'dev' environment
      $config['bootstrap'][] = 'debug';
      $config['modules']['debug'] = [
         'class' => 'yii\debug\Module',
      ];
      $config['bootstrap'][] = 'gii';
      $config['modules']['gii'] = [
         'class' => 'yii\gii\Module',
      ];
   }
   return $config;
?>
模块控制器的路由必须以模块 ID 开头,后跟控制器 ID 和操作 ID。
步骤 6 - 要在我们的应用程序中运行 actionGreet ,我们应该使用以下路由。
hello/custom/greet
其中 hello 是模块 ID,custom 是控制器 ID,greet 是操作 ID。
步骤 7 - 现在,输入http://localhost:8080/index.php?r=hello/custom/greet,您将看到以下输出。
要点
模块应该 -
用于大型应用程序。您应该将其功能分为几组。每个功能组都可以开发为一个模块。
可重复使用。一些常用的功能,例如SEO管理或博客管理,可以开发为模块,以便您可以在将来的项目中轻松重用它们。