Yii Framework Official Series Guide Series 11--Basics: Modules

Source: Internet
Author: User
Tags yii



Note: version 1.0.3 supports modules.

A module is a standalone software unit that contains models, views, controllers, and other supported components. In many ways, the module looks like an application. The main difference is that the module cannot be deployed separately, it must exist in an application. Users can access the controllers in the module as they do with the controllers in the normal application.

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

1. Create a module

The module is organized in a directory, and the name of the directory is the unique ID of the module. The structure of the module directory is similar to the application base directory. The following is a list of fourm typical directory structures for a module:

forum/   forummodule.php            module class file   components/                contains reusable user components      views/                  A view file containing small objects   controllers/               contains controller class files      defaultcontroller.php   The default controller class file   extensions/                contains the third-party extension   models/                    contains the module class file   views/                     Contains the controller view and layout file      layouts/                contains the layout file      default/                contains the Defaultcontroller view file         index.php The            home view file

The module must have a module class that inherits from Cwebmodule. The name of the class is determined by the expression ucfirst($id).'Module' , which represents the ID of the $id module (or the directory name of the module). The module class is the central place where information can be shared between storage module code. For example, we can use the Cwebmodule::p arams Enclosure parameters and share module-level application components using Cwebmodule::components.

Tip: We can use the module builder in the GII to create a basic skeleton of a new module.

2. Using the module

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



Return Array (...    ') Modules ' =>array (' Forum ',...),    ...);


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



Return Array (...    ') Modules ' =>array (        ' Forum ' =>array (            ' postperpage ' =>20,        ),    ...);


An instance of the module can be accessed through the current active controller's module property. In the module instance, we can access the information that is shared at the module level. For example, 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;


The controller actions in the module can be moduleID/controllerID/actionID accessed via routing. For example, assuming that the above forum module has a PostController controller named, we can forum/post/create access the actions in this controller by routing create . The URL that corresponds to this route http://www.php.cn/ .

Tip: If a controller is located in a subdirectory controllers of the directory, we can still use the above routing format. For example, PostController if forum/controllers/admin you are in, we can forum/admin/post/create access the create action.

3. Nested modules

Modules can be nested in infinite levels. This means that one module can contain another module, and this other module can contain other modules. We call the former the parent module , the latter as the sub-module . The submodule must be defined in the Modules property of its parent module, just as we previously defined the module in the application configuration.

To access the controller actions in the Submodule, we should use route Parentmoduleid/childmoduleid/controllerid/actionid.

The above is the YII Framework official series of Guide Series 11--Basics: module content, more relevant content please pay attention to topic.alibabacloud.com (www.php.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.