Module Development and Analysis of Yii framework

Source: Internet
Author: User

Module Development and Analysis of Yii framework

A slightly larger project, if developed according to the webapp generated by yii. All controllers are placed under the controllers folder, and all models are placed under the models folder. If you have more than n controllers and more than n models, in this case, code maintenance is a very painful process. To avoid this situation, Yii provides a directory structure of Modules.

A module is an independent unit that includes views, controllers, and other components. The difference between a module and an application is that it cannot be deployed independently. Modules are stored in the moudules directory of the application.

Your project can be divided into n multiple modules, and each Module has its own controllers and models. Such an organizational structure is much more convenient and concise for development and management.

The modules in YII are very flexible. A module can contain sub-modules. Theoretically, the module can be infinitely nested.

The directory structure of the module (the directory structure generated below is described)

Storage directory of the modules Module
└ ── Admin is a module. The module name corresponds to the directory name and is unique. It is also the moduleid in the route.
├ ── Components used by the components Module
── Controllers includes a controller
│ ── Defacontroller. php default Controller
── Messages Internationalization
── Models model files
├ ── Class file of AdminModule. php Module
└ ── Views tries to file
── Default view
│ ── Index. php View File
── Layouts contains layout files

The basic directory structure is as follows. Of course, you can add custom items on your own.

How to Create a module (Here we use the gii generator that comes with yii to create a module)

Use the gii generator that comes with yii to create the basic structure. To enable gii, modify the following content in the config/main. php file of your application:

<? Php ...... 'modules' => array ('giii '=> array ('class' => 'System. gii. giiModule ', 'Password' => '000000', // you need to enter 'ipfilters' => array ('2017. 0.0.1 ',': 1 '),),),......

Then access the url of your app/index. php? R = gii access gii. After opening it, select the Module Generator option in the left-side menu. You will see the following picture

Enter the Module name in the Module ID. Here, enter admin and click the Preview button. As shown in, it shows you all the files that will be generated, allowing you to preview them before creating them:

Click Generate to Generate all files. Because the Web server process requires write permission, make sure that your/protected folder is writable to this application.

Configure to use this module

We configure the main configuration file protected/config/main. php. The following code needs to be modified and 'admin' is added ':

...... 'Modules' => array ('giii '=> array ('class' => 'System. gii. giiModule ', 'Password' => 'your password',), 'admin ',),......

After saving the preceding changes, our new admin module is ready for use. We can access the created module through the following address:

Your application/index. php? R = admin/default/index

Use layout in the module

We access index. php? R = admin/default/index: the module uses/protected/views/layouts/main under your application. php file, and we may want to use/protected/modules/admin/views/layouts/main. the php file allows the admin module to have an independent Layout View. We can:

Add the following code to protected \ modules \ admin \ controllers \ DefaultController. php.

Public $ layout = 'application. modules. admin. views. layouts. main ';

We set the parameters from/protected/views/layouts/main. copy the php file to/protected/modules/admin/views/layouts/and make some modifications. This gives the module an independent Layout View.

Use Assets in the module

When a new module is added, it generally contains image files, CSS files, and JavaScript files.

The module can be referenced directly from the Home Directory of the website. However, if you want to create a module that can be referenced anywhere and avoid name conflicts, you need to use assets.

The process is (here the module name is admin ):

1. Place the required resources under modules/admin/assets.

2. Then, you can use CAssetManager, Yii: app ()-> assetManager to automatically publish private resources to the website directory/assets under the public directory.

3. Yii will automatically create a random non-conflicting folder under/assets under the website directory, such as 2b31b42b, and copy the files under your modules/admin/assets Directory.

For example, if my module is Admin and the file path is obtained using the following code, modify the protected \ modules \ admin \ AdminModule. php file.

class AdminModule extends CWebModule{private$_assetsUrl;public function getAssetsUrl(){if($this->_assetsUrl===null)$this->_assetsUrl=Yii::app()->getAssetManager()->publish(Yii::getPathOfAlias('application.modules.admin.assets'));return$this->_assetsUrl;}public function setAssetsUrl($value){$this->_assetsUrl=$value;}}

Then, use $ this-> module-> assetsUrl in/protected/modules/admin/views/layouts/main. php to call your css and other files. The template file code is as follows:

<link rel="stylesheet" type="text/css" href="<?php echo $this->module->assetsUrl; ?>/css/screen.css" />

4. Through the above operation, the module can be reused multiple times as long as the admin directory is copied.

Module configuration and usage

In the configuration file/config/main. php:

You can also add parameters for Attribute initialization in the module in the configuration file, for example:

......'modules'=>array('admin'=>array('web_url'=>'www.phpernote.com'), ......

The access method in Controller is as follows:

Yii: app ()-> controller-> module-> web_url;

Articles you may be interested in
  • Php uses header () to download files. The downloaded files prompt that they are damaged and cannot be opened.
  • When CuteFTP is connected to the ftp server, the "invalid Parameters" error is displayed.
  • Php smarty Chinese interception plug-in development example
  • Performance Comparison of using in_array () foreach array_search () to find whether an array contains
  • Linux chmod (file or folder permission setting) command parameters and Usage Details
  • How to operate parent page elements on iframe subpages
  • Summary of the methods for refreshing iframe using javascript
  • Array_walk and foreach, for efficiency comparison, php Performance Optimization

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.