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.
- 1If there aren't any tutorials then you could look at existing modules to see how they've been written: github.com/Codeception/Codeception/blob/master/src/Codeception/…Joe– Joe2014-07-01 13:30:40 +00:00Commented Jul 1, 2014 at 13:30
- yeah, i've been checking on the modules but most of the configuration for Modules setting 'class' path and I couldn't find a way to organize the code for this configuration which found on YII2 documentation 'modules' => [ 'v1' => [ 'basePath' => '@app/modules/v1', ], 'v2' => [ 'basePath' => '@app/modules/v2', ], ],user2959221– user29592212014-07-02 04:57:26 +00:00Commented Jul 2, 2014 at 4:57
- 1hmmm this guide describes how to create a module stuff.cebe.cc/yii2docs/guide-structure-modules.htmlFortran– Fortran2014-07-02 10:08:17 +00:00Commented Jul 2, 2014 at 10:08
- @Fortran: You should convert your reply in an answer. It is indeed the way to do it. Basically it's the same as in Yii 1.Blizz– Blizz2014-07-02 15:11:37 +00:00Commented Jul 2, 2014 at 15:11
- It is still very confusing because the tutorial does not show how to use a module on a view.felipe.zkn– felipe.zkn2015-06-16 14:32:35 +00:00Commented Jun 16, 2015 at 14:32
2 Answers
Option 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.Inside your modules folder create a folder for your module corresponding to the Module ID.
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 } }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' => 'app\modules\home\Home', ], ], ......
Option 2
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 1Preview and Generate all files. Change application configuration as in Option 1 according to your module class.
Comments
- Install Gii in yii2. Use documentation.
- 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.
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', ], ]
Please leave comment if still have confusion. I will edit to make this more sense.