Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
371 views
in Technique[技术] by (71.8m points)

php - How to create modules in 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Option 1

  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.

  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 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
       }
    }
    
  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' => 'appmoduleshomeHome',
          ],
       ],
    ......
    

Option 2

  1. If you are using Gii module, go to module generator and enter path to module class. This would be the same as appmoduleshomeHome in option 1

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...