Also available in these languages:
DeutschEnglishEspañolFrançaisעִבְרִיתBahasa Indonesia日本語polskiPortuguêsRomâniaРусскийsvenska简体中文

Module

Note: Support for module has been available since version 1.0.3.

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.

Creating Module

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:

forum/
   ForumModule.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 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 module generator in Gii to create the basic skeleton of a new module.

Using Module

To use a module, first place the module directory under modules of the application base directory. Then declare the module ID in the modules property of the application. For example, in order to use the above forum module, we can use the following application configuration:

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

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:

return array(
    ......
    'modules'=>array(
        'forum'=>array(
            'postPerPage'=>20,
        ),
    ),
    ......
);

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 route moduleID/controllerID/actionID. For example, assuming the above forum module has a controller named PostController, we can use the route forum/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.

Nested Module

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.

$Id: basics.module.txt 2041 2010-04-11 03:54:25Z qiang.xue $
If you find any typos or errors in the tutorial, please create a Yii ticket to report it. If it is a translation error, please create a Yiidoc ticket, instead. Thank you.

Total 9 comments:

#201
sub module
by will at 4:05pm on April 16, 2009.

I followed the guide to created a sub module 'module2' under /modules folder of parent module 'module1', however, it seems the server cannot find the url module1/module2. I already tried to define something like this in main.php config file: code 'modules' => array('module1','module2') /code

OR code 'modules' => array('module1','module1/module2') /code

OR code 'modules' => array('module1','module1.module2') /code none of above works, any idea?

#210
same
by Q-efx at 4:45am on April 18, 2009.

problem how to use sub modules?

#211
Setting nested modules
by MasterAM at 4:58am on April 19, 2009.

@will, @Q-efx

I did the following in order to nest modules:

'modules'=>array(
    'module1'=>array(
        'modules'=>array(
            'module2',
            'module3'
        )
    )
)
Where modules 2 and 3 are nested within module 1
#213
Omg
by Q-efx at 4:34am on April 20, 2009.

how easy ;-)

#291
change DefaultController name in a new module
by mavines at 5:00am on May 15, 2009.

Hi,

How to change defaultcontroller.php to mycontroller.php in newly created module? YII uses DefaultController.php by default and I would like to change this to some custom name.

Thanks ;)

#381
I think Kenny's comment must be on the guide.
by shuwatto at 6:15pm on June 13, 2009.

I think Kenny's comment must be on the guide.

Qiang, would you have a consideration for it?

thanks.

#781
i use it's ok
by 3gwind at 10:51am on October 31, 2009.

yiic webapp myapp

cd myapp

yiic shell

module admin module blog exit

mkdir -p modules/admin/modules

mv blog modules/admin/modules/

vi protected/config/main.php

add the blow code ... 'modules'=>array( ... 'admin'=>array( 'modules'=>array( 'blog', ), ), .... ),

brower: http://youdomain/path/to/myapp/index.php?r=admin/blog

#1141
Not Working
by zillabyte at 2:37am on February 20, 2010.

I used the yiic to create a module called admin. looks like all the directories and files were created properly. Following the steps above I also made the changes to my main.php in the config folder. However my application can not find the module's views. I get the standard Page Not Found error. Does something more have to be done to the application that I'm missing? My controllers for the module are in the module/admin/controllers folder, which is where the yiic put them. The error message seems to imply it doesn't recognize the path to the module. Here is the error.

2010/02/20 11:33:13 error exception.CHttpException.404 exception 'CHttpException' with message 'Unable to resolve the request "admin/sitemenu/show".' in D:\yii\framework\web\CWebApplication.php:314 Stack trace:

0 D:\yii\framework\web\CWebApplication.php(120): CWebApplication->runController('admin/sitemenu/...')

1 D:\yii\framework\base\CApplication.php(135): CWebApplication->processRequest()

2 D:\yii\sites\website\index.php(11): CApplication->run()

3 {main}

#1412
yiic shell sub module
by daveline at 4:50am on April 21, 2010.

How can you use yiic shell to create a nested submodule?

to create the 1st module

module test1

to create the sub module

module test1\test2

or possibly

modules test1\modules\test2

doesn't seem to work properly because the framework is looking for the "test2" module in ./protected/modules/test1/modules/test2/Test2Module.php

neither method creates the structure properly

Your Comment:

You may enter comment using Markdown syntax.

Please login with your forum account.
Note: you must have at least ONE forum post with your account.