Yii modules module configuration

Time:2022-11-25

During the development process, in a project, the functions of the project may be divided into multiple modules. For example, if our project is a management and control system for e-commerce, then our project may be divided into a sales module and a procurement module. , financial modules, etc. At this time, if we can distinguish the project according to each module, and write the code of the specified module in a directory, in this case, is our project structure clear? In Yii, we can use modules to implement the code module language

One: modules configuration

In the configuration file main.php configuration

//Here I configure a sales module and a purchase module
'modules' => [
        'sell'=> [
            'class' => 'frontend\modules\sell\Module',
        ],
         'purchase'=> [
            'class' => 'frontend\modules\purchase\Module',
        ],
    ],

Two: Create a module directory (here I only create the sell module, other modules are created in the same way)

The modules I configured above are actually in frontend. Create a modules directory under the frontend directory, and then create a sell directory under the modules directory. There are the following files and folders under the sell directory

1: module.php (module entry file)

<?php
namespace frontend\modules\sell;
class Module extends \common\components\Module
{
    public $controllerNamespace = 'frontend\modules\sell\controllers';
}

2: controllers directory

All controller methods of this module are placed in this directory

3: models directory

This directory contains the models required by this module

4: view directory

This directory contains the view files for this module

The file structure in the sell directory is as follows:

Yii modules module configuration

According to the above method, we have completed the modules configuration and use