Yii2.0-module

Source: Internet
Author: User
Php Chinese network (www.php.cn) provides the most comprehensive basic tutorial on programming technology, introducing HTML, CSS, Javascript, Python, Java, Ruby, C, PHP, basic knowledge of MySQL and other programming languages. At the same time, this site also provides a large number of online instances, through which you can better learn programming... A module is an independent software unit that consists of models, views, controllers, and other supporting components. end users can access the controllers of the modules installed in the application body, A module is regarded as a small application subject. unlike an application subject, a module cannot be deployed separately and must belong to an application subject.

Create a module

The Module is organized into a directory called [[yii \ base \ Module: basePath | base path]. in this directory, subdirectories such as controllers, models, and views are corresponding controllers, models, views, and other code are very similar to applications. The following example shows the directory structure of a model:

Forum/Module. the php module class file controllers/contains the controller class file DefaultController. php default controller class file models/contains model class file views/contains controller view file and layout file layouts/contains layout file default/contains DefaultController controller view file index. php index View File

Module Type

Each Module has a Module class that inherits [yii \ base \ Module]. this class of files is directly stored in [[yii \ base \ Module :: basePath | base path] Directory, and can be automatically loaded. When a module is accessed, it will create a unique instance of the module class similar to the application entity instance. the module instance will help the code in the module share data and components.

In the following example, a module class is roughly defined:

Namespace app \ modules \ forum; class Module extends \ yii \ base \ Module {public function init () {parent: init (); $ this-> params ['foo'] = 'bar ';//... other initialization code ...}}

If the init () method contains a lot of initialization module property code, you can save them in the configuration and load them using the following code in init:

Public function init () {parent: init (); // from config. php load configuration to initialize the module \ Yii: configure ($ this, require (_ DIR __. '/config. php '));}

The config. php configuration file may contain the following content, similar to the application subject configuration.

  [ // list of component configurations ], 'params' => [ // list of parameters ], ];

Controller in the module

When creating a controller for a Module, the usual practice is to place the controller class in the controllers sub-namespace of the Module class namespace, which also means to put the controller class file in the Module [[yii \ base \ Module:: basePath | base path. For example, to create a post controller in the forum module in the previous section, you should declare the controller class as follows:

namespace app\modules\forum\controllers; use yii\web\Controller; class PostController extends Controller { // ... }

You can configure the [[yii \ base \ Module: controllerNamespace] attribute to customize the namespace of the controller class. if some controllers are no longer in this namespace, you can configure the [[yii \ base \ Module: controllerMap] attribute to make them accessible, which is similar to what the application body configuration does.

View in the module

The view should be placed in the views directory under the [[yii \ base \ Module: basePath | base path] of the Module, view files corresponding to controllers in the module should be placed in the views/ControllerID directory, where ControllerID corresponds to the controller ID. for example, assume that the controller class is PostController and the views/post directory under the [yii \ base \ Module: basePath | base path] Directory of the corresponding Module is used.

The module can specify the layout, which is used for rendering in the module's controller view. The layout file is stored in the views/layouts directory by default. you can configure the [[yii \ base \ Module: layout] attribute to specify the layout name. if the layout attribute name is not configured, the layout of the application is used by default.

Modules

To use a module in an Application, you only need to add the module to the list of [[yii \ base \ Application: modules | modules] attribute configured by the Application body, the following code configures the forum module for the application body:

['Modules' => ['forum' => ['class' => 'app \ modules \ forum \ module ',//... other module configurations...],],]

[[Yii \ base \ Application: modules | modules] attribute uses the module configuration array. each array key is the module ID, which identifies the unique module in the Application, the array value is used to create the module configuration.

Routing

Similar to the controller that accesses the application, the routing is also used in the addressing of the controller in the module. the routing of the controller in the module must start with the module ID, followed by the controller ID and Operation ID. For example, assume that the application uses a module named forum, and the route forum/post/index indicates the index operation of the post controller in the module. if the route contains only the Module ID, the default value is the [[yii \ base \ Module: defaultRoute] attribute of default to determine which controller/operation to use. that is to say, the routing forum may represent the default controller of the forum Module.

Access Module

In a module, you may often need to obtain the instance of the module class to access the Module ID, module parameters, module components, and so on. you can use the following statement to obtain it:

$module = MyModuleClass::getInstance();

MyModuleClass corresponds to the module class you want. the getInstance () method returns the current requested module class instance. if the module is not requested, this method returns null, note that you do not need to manually create a module class because the manually created class is different from that automatically created when Yii processes requests.

Supplement: when developing a module, you cannot assume that the module uses a fixed ID, because in applications or other modules, the module may correspond to any ID. to obtain the Module ID, use the code above to obtain the module instance, and then use $ module-> id to obtain the module ID.

You can also use the following method to access the module instance:

// Obtain the module whose ID is "forum" $ module = \ Yii ::$ app-> getModule ('forum '); // Obtain the module that processes the current request controller $ module = \ Yii: $ app-> controller-> module;

The first method is valid only when you know the Module ID, and the second method is used when you know the controller that processes the request.

Once a module instance is obtained, you can access the parameters and components registered to the module, for example:

$maxPostCount = $module->params['maxPostCount'];

Boot module

Some modules run under each request. this is the case in [[yii \ debug \ Module | debug, add this module to the [[yii \ base \ Application: bootstrap | bootstrap] attribute of the Application subject.

For example, the application subject configuration in the following example ensures that the debug module is loaded every time:

[ 'bootstrap' => [ 'debug', ], 'modules' => [ 'debug' => 'yii\debug\Module', ], ]

Module nesting

The module can be nested infinitely. that is to say, the module can contain another module that contains the module. we call the former as the parent module and the latter as the child module, the sub-Module must be stated in the [[yii \ base \ Module: modules | modules] attribute of the parent Module. for example:

Namespace app \ modules \ forum; class Module extends \ yii \ base \ Module {public function init () {parent: init (); $ this-> modules = ['admin' => [// A shorter namespace 'class' => 'app \ modules \ forum \ modules \ admin \ should be used here \ module ',],];}

In the nested module controller, its route should contain the IDs of all its ancestor modules. for example, forum/admin/dashboard/index represents the index operation of the dashboard controller in the module forum submodule admin.

Best practices

Modules are used frequently in large projects. the features of these projects can be grouped. Each group contains some strongly related features, each feature group can be made into a module for development and maintenance by specific developers and development teams.

In feature groups, using modules is also a good way to reuse code. some common features, such as user management, comment management, can be developed into modules, which is very easy to reuse in related projects.

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.