Step by Step Todo module Chapter1 - mymagic/open_hub GitHub Wiki
Chapter 1
At the end of this chapter, you will have a basic working TODO module.
Getting started with boilerplateStart
BoilerplateStart serve as a template for new module creation easily using CLI.
- Run command in CLI:
cd protected
php yiic boilerplate create --name="Todo"
- Edit
TodoModule.phpfile- We going to create a new database table structure called
todo, this has to be done in code thruinstallDbfunction:
- We going to create a new database table structure called
public function installDb($forceReset = false)
{
$migration = Yii::app()->db->createCommand();
// when force reset, database will be purge and all data lost permanently
if ($forceReset) {
if (Yii::app()->db->schema->getTable('todo', true)) {
$migration->dropTable('todo');
}
}
$migration->createTable('todo', array(
'id' => 'pk',
'user_id' => 'integer NOT NULL',
'title' => 'string NOT NULL',
'text_content' => 'text NULL',
'json_extra' => 'text NULL',
'date_added' => 'integer',
'date_modified' => 'integer',
));
$migration->alterColumn('todo', 'title', 'varchar(255) NULL');
$migration->createIndex('user_id', 'todo', 'user_id', false);
$migration->addForeignKey("fk_todo-user_id", "todo", "user_id", "user", "id", "RESTRICT", "RESTRICT");
return true;
}
For more information on BoilerplateStart, please refer to Quick Start with Boilerplate
Setup configuration
- Go to
protected/modules/todo/configfolder - Looking at both your
base.phpconfig files, atmodules > todo,- Make sure everything in
modules > todo > modelBehaviorsare commented, we will come back to this later in chapter 2 - you now have 2 variables. These variable must reflect in your
TodoModule.phptoo.
- Make sure everything in
<?php
// use camelcase for class name with first character in uppsercase
class TodoModule extends WebModule
{
public $defaultController = 'frontend';
private $_assetsUrl;
public $oauthSecret;
public $var1;
public $var2;
}
- Edit about.yaml
- Set
codevalue totodo - Give it a version, lets set
1.0 - Set
titlevalue to 'My TODO Module` - Set
onelinervalue to 'My first custom module to manage todo list'. It should be short and descriptive to help other users and developers to easily understand what your module function - Set
lastUpdateto current date inYYYY-MM-DDformat, e.g.2020-02-08 - Under
developer, set theauthor,organization,emailto your detail
- Set
- Do not copy
console.dist.phptoconsole.phpandmain.dist.phptomain.php. This will be done automatically when you install the module
For more information on module configuration, please refer to Module Config
For more information on module installation, please refer to Module Install & Upgrade
Some clean up
Go to views folder
- Edit
frontend\index.php, change the file content to
Hi, I am TODO module frontend index '<?php echo $this->module->var1 ?>'
Congratulation, you made it!
You are now ready to install this new module you have just created.
- Go to
https://mydomain.com/sys/module/admin - Click on the 'New Modules' tab
- Click Initialize button on the Todo module to initialize your module
- Click on the 'Modules' tab
- Click Install button on the Todo module to install your module
After installing the module from backend, you may access your module frontend with url: https://mydomain.com/todo
Next Chapter
Chapter 2 - Add in CRUD function in backend