» » » How to create the module in Yii2

How to create the module in Yii2

posted in: Yii2.0 framework
3 min read

Recently I have been adding the new features to my own application. I add new features using the module so I can disable and enable each module easily in the application configuration. The module is self-contained software unit that contains models, controllers and views and components. I prefer to use Gii module to create the new module. I setup my application with the advanced template and I build my application in the frontend root.


Gii module URL(with enable “pretty url” in application configuration):


gii module


Here the steps I use.

  1. Create Module by Gii
  2. Create Model by Gii
  3. Create CRUD by Gii


The module structure

let says I create the timesheet module.


|- timesheet

   |- controllers















Step 1: create new module in Gii

After the module is created, you need to enable the module by adding the new module class in the application configuration. Just like the green box message above. Notice that, you don’t have the model class yet. Let create one in the next step.


Step 2: create new model by Gii

  • Before creating new model class, you need to have the actual table in the database first. Let’s create the new ma_timesheet table by Yii2 migration or you can manually create the table in phpmyadmin if you prefer. In my case, I like to create and maintain the database schema by Yii2 migration. In the terminal, I will add the migration command for creating the new migration file below.
./yii migrate/create create_new_timesheet_table

Then you can add the table structure in the new file where locates in “console/migrations/*.php“. More information about migration can be found here.

  • Now you should have the ma_timesheet table in your database. Let create the model class for our new table by Gii.

Now you will see the new model class for ma_timesheet table. Next step, we will create the controllers and views for the module by Gii. We will override the existing controllers and views that we already created by module generator.


Step 3: create CRUD by Gii

namespace error

If you get some error message regarding the namespace of the model class that your new table refers the key to(foreign keys I mean). You must add the namespace for that model class. Below is my new Timesheet model class and I add three foreign keys from the three tables which are Project, Task and User.

Project and Task tables are created using the module and all modules I create, I add them in “frontend\modules” path. So I add my namespace with “\frontend\modules\project\models\” and “\frontend\modules\task\models\”. But the User table I didn’t create as module since it is a main table in my application so the namespace is “\frontend\models\”.

In Yii2 framework, namespace is very important.

    public function rules()
        return [
            [['work_date', 'project_id', 'task_id', 'note', 'user_id', 'created_at'], 'required'],
            [['work_date', 'created_at', 'updated_at'], 'safe'],
            [['project_id', 'task_id', 'hours', 'user_id'], 'integer'],
            [['note'], 'string'],
            [['status'], 'string', 'max' => 10],
            [['project_id'], 'exist', 'skipOnError' => true, 'targetClass' => \frontend\modules\project\models\Project::className(), 'targetAttribute' => ['project_id' => 'id']],
            [['task_id'], 'exist', 'skipOnError' => true, 'targetClass' => \frontend\modules\task\models\Task::className(), 'targetAttribute' => ['task_id' => 'id']],
            [['user_id'], 'exist', 'skipOnError' => true, 'targetClass' => \frontend\models\User::className(), 'targetAttribute' => ['user_id' => 'id']],

That’s it.


Access the module

Now I can access my new timesheet module with this link:






You should edit each files we create in the modules folder to fit your needs.


Creating URLs when your application enables your modules

If your application disables your modules, you can use the URL helper to create the URL like below.

use yii\helpers\Url;

echo Url::to(['site/index']);

If your application enables your modules just like my post, you must add the leading slash in front of the controller ID like below.

use yii\helpers\Url;

// add the leading slash in front of "site" which is the controller ID
echo Url::to(['/site/index']);

If no leading slash is added, the module ID will be added before the controller ID automatically. See the sample below.



echo Url::to([‘/site/index’]);





echo Url::to([‘site/index’]);