Understanding Yii2 Directory Structure

Yii2 follows a well-organized directory structure that adheres to the Model-View-Controller (MVC) design pattern. Whether you are using the Basic Template or the Advanced Template, understanding the directory structure is crucial for efficient development.

1. Yii2 Basic Template Directory Structure

The Basic Template is suitable for small to medium-sized applications and has the following structure:

yii2-app-basic/
│── assets/  
│── commands/  
│── config/  
│── controllers/  
│── models/  
│── runtime/  
│── tests/  
│── vendor/  
│── views/  
│── web/  
│── widgets/  
│── yii  

Key Directories in Basic Template:

  • assets/ → Contains asset bundles (CSS, JS, etc.).
  • commands/ → Stores console command classes (for CLI operations).
  • config/ → Holds configuration files (web.php, console.php, db.php).
  • controllers/ → Contains controller classes that handle user requests.
  • models/ → Stores model classes for interacting with the database.
  • runtime/ → Stores temporary files such as logs and cache.
  • tests/ → Contains automated test scripts.
  • vendor/ → Third-party dependencies managed by Composer.
  • views/ → Holds view files for rendering UI.
  • web/ → The entry point (index.php) and public assets (CSS, JS, images).
  • widgets/ → Contains reusable UI components.
  • yii → The Yii2 framework bootstrap file.

2. Yii2 Advanced Template Directory Structure

The Advanced Template is designed for enterprise applications and consists of separate frontend and backend sections, along with a shared directory for common code.

yii2-app-advanced/
│── backend/  
│── common/  
│── console/  
│── environments/  
│── frontend/  
│── vendor/  

Key Directories in Advanced Template:

  • backend/ → Contains controllers, models, views, and assets for the admin panel.
  • frontend/ → Contains controllers, models, views, and assets for the main website.
  • common/ → Stores code shared between frontend and backend (e.g., models, utilities).
  • console/ → Stores Yii2 console applications (for migrations, CRON jobs).
  • environments/ → Configuration files for different environments (dev, prod).
  • vendor/ → Third-party dependencies (like in Basic Template).

Key Configuration Files in Advanced Template:

  • common/config/main.php → Common application settings.
  • common/config/main-local.php → Localized settings (like database credentials).
  • backend/config/main.php → Backend-specific configurations.
  • frontend/config/main.php → Frontend-specific configurations.
  • console/config/main.php → Console application configurations.

3. Yii2 Follows the MVC Pattern

Yii2 is based on MVC architecture, which separates an application into three interconnected parts:

  • Model (models/) → Manages data and business logic.
  • View (views/) → Handles the presentation layer (UI).
  • Controller (controllers/) → Receives user requests, processes them, and returns responses.

For example, when a user visits site/index:

  1. The controller (SiteController.php) processes the request.
  2. The model (Post.php) fetches data from the database (if needed).
  3. The view (views/site/index.php) renders the response.

4. Best Practices for Organizing Code

  • Follow Yii2’s directory structure to maintain code clarity.
  • Place reusable components in widgets/.
  • Keep business logic separate by using models/ instead of writing queries inside controllers.
  • Store configuration in config/ instead of hardcoding values.

This tutorial provides a comprehensive understanding of Yii2’s directory structure. In the next tutorial, we will explore Yii2's configuration files and environment settings to customize our application.