Application structure
Yii2's application structure is very clear, precise, and redundant (for advanced applications).
The contents of the basic
folder should be as follows:
Folder names |
Description |
---|---|
|
This includes the files ( |
|
This includes the controllers used from the command line. |
|
This includes the controllers used from web. |
|
This is the mail layout repository. |
|
This includes the models used in the whole application. |
|
This is used from Yii2 to store runtime data as logs. |
|
This includes all the test's repositories (unit, functional, fixtures, and so on). |
|
This includes the third-party module repositories managed by Composer. |
|
This contains PHP files, divided into folders that refer to controller names, used to render the main content of the page template. It is mainly called from the controller's actions to render the display output. A folder named layout contains the page template's PHP files. |
|
This is the entry point from web |
Open web/index.php
to view content:
<?php // comment out the following two lines when deployed to production defined('YII_DEBUG') or define('YII_DEBUG', true); defined('YII_ENV') or define('YII_ENV', 'dev'); require(__DIR__ . '/../vendor/autoload.php'); require(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php'); $config = require(__DIR__ . '/../config/web.php'); (new yii\web\Application($config))->run();
Here, the first two constant definitions are very important.
YII_DEBUG
defines whether you are in debug mode or not. If we set this, we will have more log information and will see the detail error call stack.
YII_ENV
defines the environment mode we are working in, and its default value is prod
. The available values are test
, dev
, and prod
. These values are used in configuration files to define, for example, a different DB connection (local database different from remote database) or other values, always in configuration files.
Since we are at the start of our project, it is recommended to set YII_DEBUG
to true
, in order to have more detailed information in case we make a mistake in our code, instead of the unhelpful, blank.
The following table contains a list of all Yii2's objects:
Objects |
Description |
---|---|
Models, Views, and Controllers |
These are the common objects to apply the MVC pattern to:
|
Components |
These are objects that contain logic. The user can write his own components to create reusable functionalities. For example, a component could be a currency converter object, which can be used at many instances in our application. |
Application Components |
They are singletons that can be called at any point in the app. Singleton means an object instanced just one time in the entire application (so the object will always be the same). The difference between Application Components and Components is that the first can have just one instance in the whole application. |
Widgets |
These view reusable objects, containing both logic and rendering code. A widget could be, for example, a box displaying today's weather info. |
Filters |
These are objects that run before or after the execution of Controller actions. A filter can be used to change the format response output of the page, for example, from HTML to JSON. |
Modules |
This contains all the objects of an app, such as Models, Views, Controller, Components, and so on; we can consider them as subapp, containing reusable sections (for example, user management). |
Extensions |
Extensions are modules packaged, that we can easily manage using Composer. |