YII Structure

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

Yii Structure

Yii2 has a new and improved application structure. It uses composer to manage its dependencies. Yii1 has only a basic application structure. While Yii2 has basic as well as advanced application structure.

Yii2 divides the entire application into following sections:

  • Backend : For backend web development

  • Common : Include common files for all the application

  • Console : Console application

  • Environments : Environment configurations

  • Frontend : For frontend web development


Yii2 Directory Structure

  1. ROOT  

  2.     /                   contains the frontend entry script and web resources  

  3.     /assets             contains the frontend web assets  

  4. common  

  5.     config/             contains shared configurations  

  6.     mail/               contains view files for e-mails  

  7.     models/             contains model classes used in both backend and frontend  

  8.     tests/              contains various tests for objects that are common among applications  

  9. console  

  10.     config/             contains console configurations  

  11.     controllers/        contains console controllers (commands)  

  12.     migrations/         contains database migrations  

  13.     models/             contains console-specific model classes  

  14.     runtime/            contains files generated during runtime  

  15.     tests/              contains various tests for the console application  

  16. backend  

  17.     assets/             contains application assets such as JavaScript and CSS  

  18.     config/             contains backend configurations  

  19.     controllers/        contains Web controller classes  

  20.     models/             contains backend-specific model classes  

  21.     runtime/            contains files generated during runtime  

  22.     tests/              contains various tests for the backend application  

  23.     views/              contains view files for the Web application  

  24.     web/                contains the entry script and Web resources  

  25. frontend  

  26.     assets/             contains application assets such as JavaScript and CSS  

  27.     config/             contains frontend configurations  

  28.     controllers/        contains Web controller classes  

  29.     models/             contains frontend-specific model classes  

  30.     runtime/            contains files generated during runtime  

  31.     tests/              contains various tests for the frontend application  

  32.     views/              contains view files for the Web application  

  33. vendor/                 contains dependent 3rd-party packages  

  34. environments/           contains environment-based overrides 

assets

Asset bundles are used to include JavaScript and style sheets. Lot of assessment and caching is done through the assets.

There is one more asset folder inside the web directory. Yii uses this folder to cache the assets. It will have a .gitignore in this folder.

If JavaScript or CSS files are need to be updated, all the folders in this directory need to be deleted. They can be deleted any time and can be auto-generated by Yii as cache files.

commands

This directory allows you to create Yii management scripts to run. These commands can be executed on command line by going on Yii root directory and typing php yii or ./yii. It will display a by default available commands list .

config

The config folder includes configuration settings which include e-mail sending, database connection, etc.

controllers

Controllers manage data traffic in MVC framework. When request is made, it is controller that processes that request.

mail

It stores templates that Yii uses to construct a mail.

models

Models manage all the database work in MVC. Any type of coding related to database is written in model.

runtime

This folder is used during processing of a web requests.

tests

This folder checks the functionality.

vendor

Yii source files reside in this directory. Third party installed module will be stored here. During upgradation, codes in this folder are overwritten hence code alteration in this directory should be avoided.

views

Views in MVC contains the pages which are displayed upon a web request. All the HTML coding is done in view directory.

web

This is the document root directory that the web server points to. The index.php file launches the Yii process when called. In this file, debugging code can be turned on or off. Debug bar is viewable at the bottom of the page.

Here you can put any files, images or anything else which needs web accessibility. Files placed in this folder only will be accessed.

Inside this web directory a subdirectory named asset is also present. This directory is used to respond web requests.