如何在 YII2 中创建模块

2022-01-07 00:00:00 php yii2

如何在 yii2 中创建模块并在配置上设置相同的模块.我在谷歌上搜索了一段时间,但找不到那么多教程.请帮忙.

How to create a module in yii2 and setting up the same on configuration. I've been searching a while on google and I cannot find that much tutorial on it. Please help.

推荐答案

选项 1

  1. 在您的应用程序基本路径上创建一个模块文件夹.这将与您当前运行的应用程序的 @app 别名相对应.这与高级模板中的基本模板或后端/前端的根文件夹相同.

  1. Create a modules folder on your application base path. This would be what corresponds to your @app alias of your currently running application. This is the same as the root folder of basic template or backend/frontend in the advanced template.

在您的模块文件夹中,为您的模块创建一个与模块 ID 相对应的文件夹.

Inside your modules folder create a folder for your module corresponding to the Module ID.

你的模块类应该在这个模块文件夹中并且应该扩展yiiaseModule.这是您的模块类的基本工作示例.

Your Module Class should be inside this module folder and should extend yiiaseModule. This is a basic working example for your module class.

<?php

namespace appmoduleshome;

class Home extends yiiaseModule
{
   public $controllerNamespace = 'appmoduleshomecontrollers';

   public function init()
   {
       parent::init();

       // custom initialization code goes here
   }
}

  • 在同一文件夹中创建模块控制器、模型和视图文件夹.

  • Create your module controller, models and views folder on the same folder.

    要访问该模块,您需要将其添加到您的应用程序配置中:

    To access the module, you need to add this to your application configuration:

    <?php
    ......
       'modules' => [
          'home' => [
             'class' => 'appmoduleshomeHome',
          ],
       ],
    ......
    

  • 选项 2

    1. 如果您使用 Gii 模块,请转到模块生成器并输入模块类的路径.这将与选项中的 appmoduleshomeHome 相同1

    预览并生成所有文件.根据您的模块类更改应用程序配置,如选项 1 中所述.

    Preview and Generate all files. Change application configuration as in Option 1 according to your module class.

    相关文章