Yii Framework official Series Guide series 11-Basic Knowledge: Module

Source: Internet
Author: User
Note: the supported modules are available from version 1.0.3. A module is an independent software unit that contains models, views, controllers, and other supported components. In many ways, a module looks like an application. The main difference is the model...



Note:Supported modules from version 1.0.3.

A module is an independent software unit that contains models, views, controllers, and other supported components. In many ways, a module looks like an application. The main difference is that a module cannot be deployed independently and must exist in an application. Users can access controllers in the module as they access controllers of common applications.

Modules are useful in some scenarios. For large applications, we may need to divide them into several modules, each of which can be maintained and deployed separately. Some common functions, such as user management and comment management, can be developed in the form of modules so that they can be easily reused in future projects.

1. create a module

The module is organized in a directory. the directory name is the unique ID of the module. The structure of the module directory is similar to that of the basic application directory. AfourmThe typical directory structure of the module:

Forum/ForumModule. php module-class file components/contains reusable user components views/view files containing small objects controllers/contains controller-class file DefaultController. php default controller file extensions/contains third-party extension models/contains module file views/contains controller view and layout file layouts/contains layout file default/contains defacontroller controller view file index. php homepage view file

The module must have a module class inherited from the CWebModule. Class name through expressionucfirst($id).'Module'OK.$idThe ID of the module (or the directory name of the module ). The module class is the central location where information can be shared between storage module codes. For example, we can use the CWebModule: params storage module parameters, and use the CWebModule: components shared module-level application components.

Tip:We can use the module creator in Gii to create the basic skeleton of the new module.

2. Modules

To use the module, first place the module directory in the basic application directorymodules. Then declare the Module ID in the modules attribute of the application. For exampleforumModule, we can use the following application configuration:



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


The module can also carry an initial attribute value during configuration. The procedure is similar to configuring application components. For example,forumThe module can have a class namedpostPerPage, Which can be configured in the application configuration as follows:



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


The module instance can be accessed through the module attribute of the current active controller. In a module instance, we can access information shared at the module level. For examplepostPerPageInformation, 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;


The controller action in the module can be routed throughmoduleID/controllerID/actionIDAccess. For example, assume thatforumThe module has a namePostControllerController, we canforum/post/createAccesscreateAction. The URL corresponding to this route is#.

Tip:If a controller is located incontrollersIn the subdirectory of the Directory, we can still use the preceding routing format. For example, assume thatPostControllerLocated inforum/controllers/admin, We can useforum/admin/post/createAccesscreateAction.

3. Nested modules

The module can be nested infinitely. This means that one module can contain another module, and the other module can contain other modules. We call the formerParent moduleThe latter isSub-module. The sub-module must be defined in the modules attribute of its parent module, just as we have previously defined the module in application configuration.

To access the controller action in the sub-module, we should use the route parentModuleID/childModuleID/controllerID/actionID.

The above is the Yii Framework official Series Guide series 11-basic knowledge: the content of the module. For more information, refer to PHP Chinese network (www.php1.cn )!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.