Home > Article > Backend Development > Yii Framework Official Series Guide Series 11 - Basics: Modules
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.
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 首页视图文件
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.
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',...), ...... );
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, ), ), ...... );
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;
Controller actions in the module can be accessed through routing
. 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/
.
If a controller is located atModules can be nested infinitely. This means that one module can contain another module, and this other module can contain other modules. We call the formercontrollers 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
createaction through
forum/admin/post/create.
3. Nested modules
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)!