Home > Backend Development > PHP Tutorial > Yii Framework Official Series Guide Series 11 - Basics: Modules

Yii Framework Official Series Guide Series 11 - Basics: Modules

黄舟
Release: 2023-03-05 17:42:02
Original
1330 people have browsed it



Note: Modules are supported starting from version 1.0.3.

A module is an independent software unit that contains models, views, controllers and other supporting components. In many ways, a module looks like an application. The main difference is that a module cannot be deployed individually, it must exist within an application. Users can access controllers in a module just like they would access controllers in a normal application.

Modules are useful in some scenarios. For large applications, we may need to divide it into several modules, and each module can be maintained and deployed independently. 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, and the name of the directory is the unique ID of the module. The structure of the module directory is very similar to the application base directory. The following lists the typical directory structure of a fourm module:

forum/
   ForumModule.php            模块类文件
   components/                包含可复用的用户组件
      views/                  包含小物件的视图文件
   controllers/               包含控制器类文件
      DefaultController.php   默认的控制器类文件
   extensions/                包含第三方扩展
   models/                    包含模块类文件
   views/                     包含控制器视图和布局文件
      layouts/                包含布局文件
      default/                包含 DefaultController 的视图文件
         index.php            首页视图文件
Copy after login

The module must have a module class that inherits from CWebModule. The name of the class is determined by the expression ucfirst($id).'Module', where $id represents the ID of the module (or the directory name of the module). Module classes are the central place for storing information that can be shared between module code. For example, we can use CWebModule::params to store module parameters and use CWebModule::components to share module-level application components.

Tips: We can use modules in Gii to create The basic skeleton of a new module created by the tool.

2. Use modules

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



##

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


Modules can also be configured with initial property values. The approach is very similar to configuring application components. For example, the

forum module can 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,
        ),
    ),
    ......
);
Copy after login


Instances of modules are accessible through the module property of the currently active controller. Within a module instance, we can access information shared at the module level. For example, to access the

postPerPage information above, 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;
Copy after login


Controller actions in the module can be accessed through routing

moduleID/controllerID/actionID

. For example, assuming the above forum module has a controller named PostController, we can access the content in this controller through the route forum/post/create create Action. The URL corresponding to this route is http://www.php.cn/.

## Tip:
If a controller is located at

controllers In subdirectories of the directory, we can still use the above routing format. For example, assuming PostController is located in forum/controllers/admin, we can access the create action through forum/admin/post/create. 3. Nested modules

Modules can be nested infinitely. This means that one module can contain another module, and this other module can contain other modules. We call the former

parent module

and the latter

submodule. The submodule must be defined in the modules attribute of its parent module, just like we defined the module in the application configuration earlier. To access controller actions in child modules, we should use the route parentModuleID/childModuleID/controllerID/actionID.

The above is the Yii Framework official series guide series 11 - Basic knowledge: module content. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template