Module

A module is a self-contained software unit that consists of models, views, controllers and other supporting components. In many aspects, a module resembles to an application. The main difference is that a module cannot be deployed alone and it must reside inside of an application. Users can access the controllers in a module like they do with normal application controllers.

Modules are useful in several scenarios. For a large-scale application, we may divide it into several modules, each being developed and maintained separately. Some commonly used features, such as user management, comment management, may be developed in terms of modules so that they can be reused easily in future projects.

A module is organized as a directory whose name serves as its unique ID. The structure of the module directory is similar to that of the application base directory. The following shows the typical directory structure of a module named forum:

A module must have a module class that extends from CWebModule. The class name is determined using the expression ucfirst($id).'Module', where $id refers to the module ID (or the module directory name). The module class serves as the central place for storing information shared among the module code. For example, we can use CWebModule::params to store module parameters, and use CWebModule::components to share application components at the module level.

Tip: We can use the yiic tool to create the basic skeleton of a new module. For example, to create the above forum module, we can execute the following commands in a command line window:

A module can also be configured with initial property values. The usage is very similar to configuring application components. For example, the forum module may have a property named postPerPage in its module class which can be configured in the application configuration as follows:

The module instance may be accessed via the module property of the currently active controller. Through the module instance, we can then access information that are shared at the module level. For example, in order to access the above postPerPage information, we can use the following expression:

$postPerPage=Yii::app()->controller->module->postPerPage;
// or the following if $this refers to the controller instance// $postPerPage=$this->module->postPerPage;

A controller action in a module can be accessed using the routemoduleID/controllerID/actionID. For example, assuming the above forum module has a controller named PostController, we can use the routeforum/post/create to refer to the create action in this controller. The corresponding URL for this route would be http://www.example.com/index.php?r=forum/post/create.

Tip: If a controller is in a sub-directory of controllers, we can still use the above route format. For example, assuming PostController is under forum/controllers/admin, we can refer to the create action using forum/admin/post/create.

Modules can be nested. That is, a module can contain another module. We call the former parent module while the latter child module. Child modules must be placed under the modules directory of the parent module. To access a controller action in a child module, we should use the route parentModuleID/childModuleID/controllerID/actionID.