Understanding The Directory Structure

Yii 2 Directory Structure

In the previous part, we downloaded the Yii 2 Advanced template and added it to our document root directory. Now we'll just take a glimpse of the entire directory structure of the template.

The basic and advanced template differs a lot in terms of the directory structure. As said before, we'll just focus on the advanced template.

First, let's check what happens when we just open the advanced template in the browser. So now if you have put the advanced directory correctly into the document root, you can visit your http://localhost/advanced

Yii 2 Directory Structure - Localhost

Here, we'll cover the just the necessary directories and files. In advanced series, we will go in depth with each of these.

 

backend

While development of any web application, we have two things to be considered, the consumer/user facing side of the website and the admin panel or the dashboard.

The backend directory serves the purpose of the admin panel or the actions which are restricted to the business logic. For example, in a blog like Codegear, backend users can edit, delete and publish the articles whereas the frontend users can just comment on the articles.

Organising the frontend and backend gives web application a better security, give better and organised arrangement. This way we can launch features as per the need of the users or the admins.

Now, take a look at the backend directory. Click on the backend directory and here is what you see...

Yii 2 Backend Directory Structure

assets

This directory consists the configurations for the assets managed in the backend project. By default, it has AppAsset.php file inside, which inculdes the configurations of the javascript and the css files.

 

config

Here we have all the configuration files. These files return the PHP Array. The config variables can be configured depending on the environment of the application.

 

controllers

In the MVC Pattern of coding, the C stands for Controllers. These controllers are present in the controller's directory. Yii follows the convention of <NameController> for naming the controllers. Example, UserController.php is the name of the file and the class is UserController. We will get back to this in the next articles on controllers.

 

models

Models are the classes which contain the 'business logic'. When we say business logic it's the processing of data, manipulating the values, etc. In Yii, this directory also has classes which are a link to the Database through which we can access the database tables. These models and tables have one to one mapping.

 

runtime

This directory is used to store the logs, the cached pages or other files. Stores dynamically generated files.

 

views

Views store the files which are displayed via a browser. The data is passed from the controller in the view and then sent to the browser in HTML format. Thus separating the business logic in Models and display logic in Views.

 

web

This is the root directory of the project. We will point our domain to this directory

 

common

Now take a look at the common directory which is located inside the advanced directory...

As the name suggests, the common directory has the config, models, etc. which are used in the backend and the frontend projects. For example, you can have database configuration in common/config if, both of them uses the same database. Also, the business logic which are common, can be put in models directory. Most of the time, I put the models (ActiveRecord) into common/models because the entire application uses the same database and the tables.

Common Directory

frontend

As the backend is for the admins, we have the frontend for the users of the web application. The directory structure and the working of the frontend and the backend is exactly same.

 

console

If you want to run a process, cron or any other scrip using terminal this is the thing you are looking for. Your config, controllers, models, runtime, etc. all have the same functionality as that of the frontend or the common. There are few technalities like the classes they extend and stuff which we'll see in the upcomming articles. The console applications are run using the yii file in the main directory.

Console Directory

environments

Now, while development you want to show the error or debug messages which is not the case in the production environment. Also, depending on the environment your configurations will change, example, database name, password, etc or may be the variables used for the testing. This directory is used only when you initialise the Yii 2 app using the init command. Here you define the local params (main-local.php & params-local.php) and the index.php. Each directory inside the environments directory is a specific environment within itself. We'll get to this in depth further.

 

vendor

Anything you define as a requirement in the composer.json file will go into vendor directory. This way the dependencies can be managed properly and efficiently within the development team. This directory is not added to the repository (version control system) as these dependencies are defined in the composer.json and installed vai composer command.

Vendor Directory

These are the important directories which we have to understand before kickstarting any project with Yii 2 advanced template. You can configure the template as per your requirements. For a project I have executive as the one of the directories in the advanced web app, works same as the backend but only for the specific users in the company. This is hosted on different subdomain. We'll get back to the modification process in the advance series.