- Yii2 Framework
-
Introduction & Setup
- Introduction to Yii2 Framework
- Installing Yii2 (Basic & Advanced Templates)
- Understanding Yii2 Directory Structure
- Yii2 Configuration Basics
- Routing & Pretty URLs in Yii2
-
Yii2 Core Concepts
- Yii2 Application Bootstrapping & Lifecycle
- Understanding Yii2 Request & Response Handling
- Working with Yii2 Components & Helpers
- Yii2 Widgets & Using Built-in Widgets
- Yii2 Helpers & Utility Classes
-
Models & Database Operations
- Yii2 Models, Active Record & Database Connections
- CRUD Operations in Yii2
- Yii2 Query Builder & DAO (Direct SQL Queries)
- Handling Relationships in Yii2 Active Record
- Yii2 Migrations & Seeding
-
Views, Layouts & Themes
- Yii2 Views & Layouts
- Yii2 Asset Bundles & Asset Management
- Integrating Bootstrap in Yii2
- Yii2 Theme Integration
- Yii2 Custom Widgets & Reusable Components
-
Forms, Validation & Data Presentation
- Yii2 Forms & Validation
- Using Yii2 GridView & ListView Widgets
- Yii2 Pagination & Sorting
- Yii2 File Uploads
-
Security & User Management
- User Authentication in Yii2
- Role-Based Access Control (RBAC) in Yii2
- Yii2 Security Features
-
Console Commands & Advanced Features
- Yii2 Console Commands
- Yii2 Events & Behaviors
- Yii2 RESTful API Development
- Consuming Third-Party APIs in Yii2
- Yii2 Background Jobs & Queue System
-
Performance Optimization & Caching
- Yii2 Caching Techniques
- Yii2 Performance Optimization
- Debugging & Logging in Yii2
-
Deployment & Best Practices
- Deploying Yii2 Applications
- Yii2 Best Practices & Large-Scale Application Structure
- Yii2 Multilingual & Localization Support
- Yii2 Module Development
- Integrating Yii2 with Frontend Frameworks (Angular/Vue/React)
Yii2 Module Development
Yii2 provides a modular structure, allowing developers to create independent, reusable, and self-contained modules. A module in Yii2 is like a mini-application, having its own controllers, models, views, and configurations.
In this tutorial, we will cover:
- Default behavior of Yii2 modules
- Using a custom database connection inside a module
- Adding a custom theme for a module
1. Creating a Basic Yii2 Module
1.1 Generate Module via Gii
1. Run Gii
by visiting:
http://your-app.com/index.php?r=gii
2 Select Module Generator.
3. Set:
- Module ID:
admin
- Module Namespace:
app\modules\admin
4. Click Generate to create the module.
1.2 Manually Create Module Structure
Alternatively, create the module manually under modules/admin
:
modules/
admin/
controllers/
DefaultController.php
models/
views/
default/
index.php
AdminModule.php
1.3 Define the Module Class
In modules/admin/AdminModule.php
:
namespace app\modules\admin;
class AdminModule extends \yii\base\Module
{
public $controllerNamespace = 'app\modules\admin\controllers';
public function init()
{
parent::init();
// Custom initialization code
}
}
1.4 Create the Default Controller
In modules/admin/controllers/DefaultController.php
:
namespace app\modules\admin\controllers;
use yii\web\Controller;
class DefaultController extends Controller
{
public function actionIndex()
{
return $this->render('index');
}
}
1.5 Create the Default View
In modules/admin/views/default/index.php
:
<h1>Welcome to Admin Module</h1>
1.6 Register the Module in config/web.php
'modules' => [
'admin' => [
'class' => 'app\modules\admin\AdminModule',
],
],
Now, visit:
http://your-app.com/index.php?r=admin/default/index
You should see "Welcome to Admin Module".
2. Console Commands in Yii2 Module
Yii2 allows modules to have their own console commands for tasks like database migrations, cron jobs, and batch processing.
2.1 Define Console Commands in the Module
Modify AdminModule.php
to set a separate namespace for console commands:
namespace app\modules\admin;
use Yii;
class AdminModule extends \yii\base\Module
{
public $controllerNamespace = 'app\modules\admin\controllers';
public function init()
{
parent::init();
// If the request is coming from the console (CLI)
if (Yii::$app->request->isConsoleRequest) {
\Yii::configure($this, [
'controllerNamespace' => 'app\modules\admin\commands'
]);
}
}
}
2.2 Create the Console Command Directory
Inside the admin
module, create a commands
directory:
modules/
admin/
commands/
TestController.php
2.3 Create a Console Command
In modules/admin/commands/TestController.php
:
namespace app\modules\admin\commands;
use yii\console\Controller;
class TestController extends Controller
{
public function actionIndex()
{
echo "Admin module console command is working!\n";
}
}
2.4 Run the Console Command
Navigate to your Yii2 project root and run:
php yii admin/test
Output:
Admin module console command is working!
This allows each module to have isolated CLI functionalities, making it useful for background jobs and scheduled tasks.
3. Database Configuration via main-local.php
Instead of hardcoding database configurations inside the module, we can use an external configuration file (main-local.php
).
3.1 Load the Configuration in AdminModule.php
Modify AdminModule.php
to include the external config file if it exists:
namespace app\modules\admin;
use Yii;
class AdminModule extends \yii\base\Module
{
public $controllerNamespace = 'app\modules\admin\controllers';
public function init()
{
parent::init();
// Load local database config if available
if (file_exists(__DIR__ . '/config/main-local.php')) {
Yii::configure($this, require(__DIR__ . '/config/main-local.php'));
}
}
}
3.2 Create the main-local.php
Configuration File
Inside modules/admin/config/main-local.php
:
<?php
return [
'components' => [
'db' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=admin_db',
'username' => 'root',
'password' => 'Password#3',
'charset' => 'utf8',
],
],
];
3.3 Use the Custom Database Connection in Models
Modify User.php
in modules/admin/models/
:
namespace app\modules\admin\models;
use Yii;
use yii\db\ActiveRecord;
class User extends ActiveRecord
{
public static function getDb()
{
return \yii::$app->getModule('admin')->db;
}
public static function tableName()
{
return 'users';
}
}
3.4 Test the Custom Database Connection
Modify DefaultController.php
in modules/admin/controllers/
:
use app\modules\admin\models\User;
public function actionUsers()
{
$users = User::find()->all();
return $this->render('users', ['users' => $users]);
}
Now, visit:
http://your-app.com/index.php?r=admin/default/users
This method ensures that database configurations are environment-specific and easier to manage.
4. Custom Theme & Layout for the Module
Each module can have its own theme, ensuring a separate UI layout and styling.
4.1 Define the Custom Theme in AdminModule.php
Modify AdminModule.php
to configure the theme dynamically:
namespace app\modules\admin;
use Yii;
class AdminModule extends \yii\base\Module
{
public $controllerNamespace = 'app\modules\admin\controllers';
public function init()
{
parent::init();
// Apply module-specific theme
Yii::$app->view->theme = new \yii\base\Theme([
'pathMap' => ['@app/views' => dirname(__FILE__) . '/themes/views'],
'baseUrl' => dirname(__FILE__) . '/themes/views',
]);
}
}
4.2 Create a Theme Folder for the Module
Inside modules/admin/themes/
, create the structure:
modules/
admin/
themes/
views/
layouts/
main.php
views/
default/
index.php
4.3 Define a Custom Layout for the Module
Create modules/admin/themes/views/layouts/main.php
:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Admin Panel</title>
<link rel="stylesheet" href="/css/admin.css">
</head>
<body>
<header><h1>Admin Panel</h1></header>
<div class="content">
<?= $content ?>
</div>
</body>
</html>
4.4 Modify Module Views
Modify modules/admin/themes/views/default/index.php
:
<h2>Welcome to the Admin Dashboard</h2>
4.5 Test the Custom Theme
Now, visit:
http://your-app.com/index.php?r=admin
Your module will now use the custom theme and layout.
5. Module-Level Access Control
Restrict access to the admin module using Yii2 Access Control Filter (ACF).
Modify AdminModule.php
:
use yii\filters\AccessControl;
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::class,
'only' => ['*'],
'rules' => [
[
'allow' => true,
'roles' => ['@'], // Only logged-in users
],
],
],
];
}
What We Achieved?
- Basic Module Development – Created an admin module.
- Created Console Commands for the Module – Allowing CLI execution of module tasks
- Configured Database via
main-local.php
– Making database settings environment-specific - Implemented a Custom Theme & Layout – Keeping module UI separate from the main application
- Access Control – Restricted module access to authenticated users.
This approach makes the Yii2 module more modular, scalable, and reusable.
Prepare for Interview
- Debugging in Python
- Multithreading and Multiprocessing in Python
- Context Managers in Python
- Decorators in Python
- Generators in Python
- Requests in Python
- Django
- Flask
- Matplotlib/Seaborn
- Pandas
- NumPy
- Modules and Packages in Python
- File Handling in Python
- Error Handling and Exceptions in Python
- Indexing and Performance Optimization in SQL
Random Blogs
- OLTP vs. OLAP Databases: Advanced Insights and Query Optimization Techniques
- Best Platform to Learn Digital Marketing in Free
- Internet of Things (IoT) & AI – Smart Devices and AI Working Together
- Quantum AI – The Future of AI Powered by Quantum Computing
- AI in Marketing & Advertising: The Future of AI-Driven Strategies
- Big Data: The Future of Data-Driven Decision Making
- 15 Amazing Keyword Research Tools You Should Explore
- Understanding HTAP Databases: Bridging Transactions and Analytics
- Loan Default Prediction Project Using Machine Learning
- Mastering SQL in 2025: A Complete Roadmap for Beginners
- Python Challenging Programming Exercises Part 3
- Top 10 Knowledge for Machine Learning & Data Science Students
- Python Challenging Programming Exercises Part 2
- Datasets for Exploratory Data Analysis for Beginners
- The Ultimate Guide to Machine Learning (ML) for Beginners
Datasets for Machine Learning
- Ozone Level Detection Dataset
- Bank Transaction Fraud Detection
- YouTube Trending Video Dataset (updated daily)
- Covid-19 Case Surveillance Public Use Dataset
- US Election 2020
- Forest Fires Dataset
- Mobile Robots Dataset
- Safety Helmet Detection
- All Space Missions from 1957
- OSIC Pulmonary Fibrosis Progression Dataset
- Wine Quality Dataset
- Google Audio Dataset
- Iris flower dataset
- Artificial Characters Dataset
- Bitcoin Heist Ransomware Address Dataset