Yii Quick Start Guide (4): Powerful "small objects"

Source: Internet
Author: User

1. Modify the default controller.

Default yii application:

All configurations can be configured in Main. php. to modify the default controller, add the following code in Main. php:

'Defaultcontroller' => '', // modify the default Controller

Yii module: the admin module is used as an example.

Each yii module has a separate folder under the protected/modules directory, for example, admin. Then, the corresponding module file exists under the corresponding module folder, such as adminmodule. php.

The module controller that modifies the module must be defined as follows in adminmodule. php:

$ This-> defaultcontroller = 'index'; // modify the default controller of the module.

2. module configuration file

Yii's core idea is to reuse existing code as much as possible

If you do not specify the configuration file in the module, yii will find the configuration file shared by the entire yii system, that is, Main. php.

However, in most cases, the configuration items in the module are slightly different from the configuration items of the entire application. In this case, you need to configure the different items separately in the module, so yii thinks of this demand and allows you to do the same.

The code structure in adminmodule. php is as follows:

The "init" method is executed during initialization of the module, so the configuration items of the module are also implemented here.

To set the configuration file of the module separately, you need to make the following settings in the "init" method:

At this time, we may find that we use the global object in yii to change the behavior in yii. Of course, we can also use it to obtain related configuration items in yii, right!

3. "small objects" in yii"

In yii, the view layer adopts the native PHP code and HTML mixed layout template. This method removes the "resolution" step for the view layer and increases the system speed, but it also increases the difficulty of writing the view-Layer Code. However, this difficulty is relatively difficult. Generally, all programmers can accept this method.

Yii adopts this method in the view layer, which makes yii more powerful in the view layer than other frameworks. Its outstanding feature is its "small object"

Using yii's "small objects", you can easily create forms directly associated with the data model at the view layer, or create common verification codes without passing values, you can also conveniently implement data paging. Of course, you can also easily customize small objects for other functions.

4. A Preliminary Exploration of the form of "small objects"

To use a yii form, you first need to create a model one by one. There are two types of models in yii. One is that data does not need to be saved. after use, you can directly discard the following: form Model --- cformmodel

Another model is to insert the collected data into the database, and the data will not be discarded: cactiverecord

First, we can simply understand the form without data. Then we can directly create a form model,

Create: article. php In the protected/modules/admin/models folder. The Code is as follows:

Then, in the controller, we need to pass our data model to the view so that the view can use "small objects" to create a form. The controller code is as follows:

Now, we can use small objects to create a form in the View:

First, we must call the beginwidget method to create a small object class, and then determine whether to call the endwidget method as needed, for example, if a form has a start tag and an end tag, you need to call the form, but you do not need to call the create verification code IMG tag.

Then we call the "small object" instance "$ Form" to call the method in the "cactiveform" class to create the corresponding form. Here we create a "text input box" as an example, other forms are created in the same way. You need to view the corresponding methods in the "cactiveform" class and use them directly.

When using the method class in the "cactiveform" class to create a form, the called method must pass the "Three Parameters"

The first parameter is "data model object", which is an instance of the data model we passed from the Controller.

The second parameter: "form name attribute value". The value must be consistent with the field value in the database. If the value is not in the database, it must be declared in the data model. The method is as follows: in the article model

Finally, the style of the generated form is as follows:

The source code of the webpage is as follows:



Continue in the next section: yii Member registration function (cactiverecord type form creation and yii Database Operations)



Yii Quick Start Guide (4): Powerful "small objects"

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.