个性化阅读
专注于IT技术分析

YII模块介绍和用法示例

点击下载

本文概述

模块驻留在主应用程序中, 并组织为一个目录, 该目录称为模块的基本路径。该模块目录将具有自己的MVC(模型, 视图和控制器)以及其他支持组件, 就像应用程序一样。


模块结构

模块遵循如下所示的典型结构。

newModule/
    Module.php                   the module class file
    controllers/                 containing controller class files
        DefaultController.php    the default controller class file
    models/                      containing model class files
    views/                       containing controller view and layout files
        layouts/                 containing layout view files
        default/                 containing view files for DefaultController
            index.php            the index view file

模块类别

模块类特点:

  • 每个模块应具有一个扩展到yii \ base \ Module的唯一类。
  • 类应该位于模块的基本路径下并且应该可以访问。
  • 在执行时, 将创建模块类的单个实例。
  • 模块实例用于共享模块中代码的数据和组件。
  • 默认情况下, 它被命名为Module.php。

例子

让我们看一个创建模块的例子。

步骤1在Yii2文件夹的Frontend目录中创建名为folder的模块。

步骤2在modules文件夹内, 创建一个名为project的文件夹。

步骤3在项目文件夹中, 创建一个名为Project.php的文件。

<?php 

namespace frontend\modules\project; 

/** 
 * project module definition class 
 */ 
class Project extends \yii\base\Module 
{ 
    /** 
     * @inheritdoc 
     */ 
    public $controllerNamespace = 'frontend\modules\project\controllers'; 

    /** 
     * @inheritdoc 
     */ 
    public function init() 
    { 
        parent::init(); 

        // custom initialization code goes here 
    } 
}

看上面的代码, 这是我们创建的模块类。这里的init()函数用于初始化模块的属性。

步骤4在项目文件夹中, 再创建两个名为controllers和views的文件夹。

步骤5在controllers文件夹内, 创建EmpController.php文件。

<?php 

namespace frontend\modules\project\controllers; 

use yii\web\Controller; 

/** 
 * Default controller for the `project` module 
 */ 
class EmpController extends Controller 
{ 
    /** 
     * Renders the index view for the module 
     * @return string 
     */ 
    public function actionMessage() 
    { 
        return $this->render('message'); 
    } 
}

看上面的快照, 这里我们定义了动作actionMessage。

步骤6在views文件夹内创建emp文件夹, 并在其中创建message.php文件。

<h1>这是创建模块的示例。</ h1>

下面的快照显示了我们modd目录中最终模块的结构。它显示在modd / frontend / modules目录中创建的所有文件。

查看模块1

步骤7现在, 我们需要进行一些配置设置以添加我们的模块。转到前端目录的config文件夹。

将模块添加到config文件夹中的main.php文件中。

<?php 
$params = array_merge( 
    require(__DIR__ . '/../../common/config/params.php'), require(__DIR__ . '/../../common/config/params-local.php'), require(__DIR__ . '/params.php'), require(__DIR__ . '/params-local.php') 
); 

return [ 
    'id' => 'app-frontend', 'basePath' => dirname(__DIR__), 'bootstrap' => ['log'], 'controllerNamespace' => 'frontend\controllers', 'modules' => [ 
        'project' =>[ 
            'class' => 'frontend\modules\project\Project', ], ], 'components' => [ 
        'request' => [ 
            'csrfParam' => '_csrf-frontend', ], 'user' => [ 
            'identityClass' => 'common\models\User', 'enableAutoLogin' => true, 'identityCookie' => ['name' => '_identity-frontend', 'httpOnly' => true], ], 'session' => [ 
            // this is the name of the session cookie used for login on the frontend 
            'name' => 'advanced-frontend', ], 'log' => [ 
            'traceLevel' => YII_DEBUG ? 3 : 0, 'targets' => [ 
                [ 
                    'class' => 'yii\log\FileTarget', 'levels' => ['error', 'warning'], ], ], ], 'errorHandler' => [ 
            'errorAction' => 'site/error', ], /* 
        'urlManager' => [ 
            'enablePrettyUrl' => true, 'showScriptName' => false, 'rules' => [ 
            ], ], */ 
    ], 'params' => $params, ];

步骤8在浏览器中使用以下URL运行它,

http://localhost/modd/frontend/web/index.php?r = project / emp / message

查看模块 2

重要事项

  • 模块应用于大型应用。将其功能分为几个组, 并将其开发为模块。
  • 模块应可用于将来的项目。

下载此示例

赞(0)
未经允许不得转载:srcmini » YII模块介绍和用法示例

评论 抢沙发

评论前必须登录!