Yii entry guide (III): yii "module"

Source: Internet
Author: User

1. yii module Introduction

A module in yii can be considered as a separate application. It has independent controllers, models, views, and components.

Generally, we use yii for web project development, so we can use the "Default yii application" as the Web Front-end. When a background is required,

In this case, you need to create a new "module", and then let the new module play the "background" role.

Presumably, this metaphor can best illustrate the meaning of the modules in yii ......

Our project structure is as follows:

So when we access: http: // localhost/testyii/index. php In the browser? R = site/index or http: // localhost/testyii/

When the access is: protected/controllers/sitecontroller. the actionindex method in the sitecontroller class in PHP. The called view is the view file specified in the Action Method: $ this-> render ('index');. The View File is located: protected/views/site/index. PHP

The default accessed application is called the "Default yii application"

2. Magic Gii

In yii, there is such a thing as "Gii"

It can be used to automatically create modules, controllers, forms, database curd operations, and data models in yii.

I also call it the magician in yii, but I don't need it unless I create a module, because I still feel "self-help clothes and food"

3. Use Gii to create the "background module"

In yii, "separate applications" exist in the form of "modules", while "core and extension classes" exist in the form of "components ".

Therefore, Gii is a "separate application", so it exists as a "module". To use Gii, you must first

In Main. php, go to "enable Gii module". The main. php file is located at: protected/config

Open the main. php file and find the code shown in. By default, the Gii module is commented out, so you need to remove the comment.

Then access: http: // localhost/testyii/index. php? R = GII

When prompted that you need to enter the password, enter the password we set above. The logon interface is as follows:


Go to "automatically create a module". The page is as follows:

Click Generate to generate the file. The result is as follows:

After the file is generated, a "modules" folder will be added to the "protected" Directory, which contains all the "modules" we created.

In order to introduce and use this module in the project, we need to make some configuration in Main. php according to the above prompt.

Then you can access the current module through a browser: http: // localhost/testyii/index. php? R = Admin

The accessed interface is as follows:

Based on the displayed results, we will find that by default, the view file of the background module uses the layout file of our foreground application, which also shows a feature of yii: high reuse of resources

The yii system looks for layout files when you access the rendering View File of the background module. When the background module does not specify the layout file, or when the "controller" inherited by the Controller of the specified background module is searched up, that is, the "controller" under the "protected/components" default application is found. then, apply the layout file specified in the Controller to the background module interface.

What should I do to make the background module have its own layout file?

First, create a "components" folder in the background module, and then create a "controller" Controller under the folder, such:

Then, in the "controller", specify the layout file of the background module, or set the layout file of the background module to none. This process is consistent with that of the foreground layout file, I will not repeat it here.

I will set the background layout file to none here, because the Web Background usually does not need to layout the file.


Next step: Modify the default controller, set the module's separate configuration file, "background" Build and form


Yii entry guide (III): yii "module"

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.