a module is an entity that has its own models, views, controllers, and possibly other modules. it is practically an application inside the application.
step 1 − create a folder called modules inside your project root. inside the modules folder, create a folder named hello. this will be the basic folder for our hello module.
step 2 − inside the hello folder, create a file hello.php with the following code.
<?php
   namespace app\modules\hello;
   class hello extends \yii\base\module {
      public function init() {
         parent::init();
      }
   }
?>
we have just created a module class. this should be located under the module's base path. every time a module is accessed, an instance of the correspondent module class is created. the init() function is for initializing the module's properties.
step 3 − now, add two more directories inside the hello folder − controllers and views. add a customcontroller.php file to the controller’s folder.
<?php
   namespace app\modules\hello\controllers;
   use yii\web\controller;
   class customcontroller extends controller {
      public function actiongreet() {
         return $this->render('greet');
      }
   }
?>
when creating a module, a convention is to put the controller classes into the controller’s directory of the module's base path. we have just defined the actiongreet function, that just returns a greet view.
views in the module should be put in the views folder of the module's base path. if views are rendered by a controller, they should be located in the folder corresponding to the controllerid. add custom folder to the views folder.
step 4 − inside the custom directory, create a file called greet.php with the following code.
<h1>hello world from custom module!</h1>
we have just created a view for our actiongreet. to use this newly created module, we should configure the application. we should add our module to the modules property of the application.
step 5 − modify the config/web.php file.
<?php
   $params = require(__dir__ . '/params.php');
   $config = [
      'id' => 'basic',
      'basepath' => dirname(__dir__),
      'bootstrap' => ['log'],
      'components' => [
         'request' => [
            // !!! insert a secret key in the following (if it is empty) - this is
               //required by cookie validation
            'cookievalidationkey' => 'ymoayrebzha8guruoliohglk8flxckjo',
         ],
         'cache' => [
            'class' => 'yii\caching\filecache',
         ],
         'user' => [
            'identityclass' => 'app\models\user',
            'enableautologin' => true,
         ],
         'errorhandler' => [
            'erroraction' => 'site/error',
         ],
         'mailer' => [
            'class' => 'yii\swiftmailer\mailer',
            // send all mails to a file by default. you have to set
            // 'usefiletransport' to false and configure a transport
            // for the mailer to send real emails.
            'usefiletransport' => true,
         ],
         'log' => [
            'tracelevel' => yii_debug ? 3 : 0,
            'targets' => [
               [
                  'class' => 'yii\log\filetarget',
                  'levels' => ['error', 'warning'],
               ],
            ],
         ],
         'db' => require(__dir__ . '/db.php'),
      ],
      'modules' => [
         'hello' => [
            'class' => 'app\modules\hello\hello', 
         ],
      ],
      'params' => $params,
   ];
   if (yii_env_dev) {
      // configuration adjustments for 'dev' environment
      $config['bootstrap'][] = 'debug';
      $config['modules']['debug'] = [
         'class' => 'yii\debug\module',
      ];
      $config['bootstrap'][] = 'gii';
      $config['modules']['gii'] = [
         'class' => 'yii\gii\module',
      ];
   }
   return $config;
?>
a route for a module's controller must begin with the module id followed by the controller id and action id.
step 6 − to run the actiongreet in our application, we should use the following route.
hello/custom/greet
where hello is a module id, custom is a controller id and greet is an action id.
step 7 − now, type http://localhost:8080/index.php?r=hello/custom/greet and you will see the following output.
important points
modules should −
be used in large applications. you should divide its features into several groups. each feature group can be developed as a module.
be reusable. some commonly used features, as seo management or blog management, can be developed as modules, so that you can easily reuse them in future projects.