php 如何在 YII2 中创建模块

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24511481/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:20:49  来源:igfitidea点击:

How to create modules in YII2

phpyii2

提问by user2959221

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.

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

回答by Kshitiz

  1. Install Giiin yii2. Use documentation.
  2. Then use module generator. You need the web permission to create the file for the the folder OR you can copy paste the generated code and create the specified file manually.
  3. When generation is complete it will show you a green text. For try module" (When folder have the web permission) OR Copy following code to main.php configuration file under module. Replace modulename with yours.

    'modules' => [
        'modulename' => [
             'class' => 'app\modules\modulename\Module',
        ],
    ]
    
  1. 在 yii2 中安装Gii。使用文档。
  2. 然后使用模块生成器。您需要 Web 权限才能为文件夹创建文件,或者您可以复制粘贴生成的代码并手动创建指定的文件。
  3. 生成完成后,它会显示一个绿色文本。对于 try 模块”(当文件夹具有 web 权限时)或将以下代码复制到模块下的 main.php 配置文件中。将 modulename 替换为您的。

    'modules' => [
        'modulename' => [
             'class' => 'app\modules\modulename\Module',
        ],
    ]
    

Please leave comment if still have confusion. I will edit to make this more sense.

如果仍有困惑,请发表评论。我将进行编辑以使这更有意义。

回答by Zack

Option 1

选项1

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

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

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

    <?php
    
    namespace app\modules\home;
    
    class Home extends \yii\base\Module
    {
       public $controllerNamespace = 'app\modules\home\controllers';
    
       public function init()
       {
           parent::init();
    
           // custom initialization code goes here
       }
    }
    
  4. Create your module controller, models and views folder on the same folder.

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

    <?php
    ......
       'modules' => [
          'home' => [
             'class' => 'app\modules\home\Home',
          ],
       ],
    ......
    
  1. 在您的应用程序基本路径上创建一个模块文件夹。这将@app与您当前运行的应用程序的别名相对应。这与高级模板中的基本模板或后端/前端的根文件夹相同。

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

  3. 你的模块类应该在这个模块文件夹中并且应该扩展\yii\base\Module. 这是您的模块类的基本工作示例。

    <?php
    
    namespace app\modules\home;
    
    class Home extends \yii\base\Module
    {
       public $controllerNamespace = 'app\modules\home\controllers';
    
       public function init()
       {
           parent::init();
    
           // custom initialization code goes here
       }
    }
    
  4. 在同一个文件夹中创建你的模块控制器、模型和视图文件夹。

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

    <?php
    ......
       'modules' => [
          'home' => [
             'class' => 'app\modules\home\Home',
          ],
       ],
    ......
    

Option 2

选项 2

  1. If you are using Gii module, go to module generator and enter path to module class. This would be the same as app\modules\home\Homein option 1

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

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

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