A module is a self-contained unit which contains models, views, controllers and other supporting components. It is same like an application, but the difference is module can’t be deployed alone.

Modules are useful units in large applications. We can easily develop and maintain the large application by dividing it into several modules and we can use the common modules for several applications.

Creating module: Every module should have unique name in one application and the module should be organized as a folder. The typical directory structure of a module will be like:

For example our module name is ‘abc’, the folder structure is as follows:

abc
AbcModule.php the module class file
components containing reusable user components
views containing view files for widgets
controllers containing controller class files
DefaultController.php the default controller class file
extensions containing third-party extensions
models containing model class files
views containing controller view and layout files
layouts containing layout view files
default containing view files for DefaultController
index.php the index view file

 

A module should have module class which extends from CWebModule.

Using Module:

Place the module directory under the modules folder,then declare the module id in the modules property of the application.

 return array(
......
'modules'=>array('abc',...),
......
);

 

The module instance may be accessed via the module property of the currently active controller.

A controller action in a module can be accessed using the route moduleID/controllerID/actionID. For example, assuming the above abc module has a controller named PostController, we can use the route abc/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=abc/post/create.