Learn the YII2 framework from scratch (v) Quick Build Code tools GII use _php instance

Source: Internet
Author: User
Tags generator yii

The YII2 framework is called a framework for efficient and rapid development because there is a magic tool in GII

As the coder of the YII1 framework knows, we can quickly generate code for you, that is, to build a webapp that can be modified or deleted, without writing a single line of code.

Of course, as a coder, do not write code to achieve the functions we want to do.

Last describes how to install the YII framework, this time describes how to use the GII tool to quickly implement CRUD functionality.

After the framework installation is complete, you can access the GII tool through the following links

Http://localhost/yii2test/backend/web/index.php?r=gii

YII2 's GII tool uses bootstrap, compared to YII1 looks a lot nicer.

The YII2 Framework provides 6 build tools: Model,crud,controller,form,module,extension

I have installed a yii2-kartikgii plug-in that is used to generate the Crud method of the Kartik pattern.

This time take Kartik crud with the default build tool to explain.

Generation of modules

If you want to implement application modularity, then module generator can help you. Click Module Generator Generation Module

Generate a module named "User"


Clicking Preview will help us generate three files and click the link to preview the code.

Click Generate to generate code.

Code generation complete, need to configure file backend/config/main.php, add modules to the application.


The browser enters the following address:

Http://localhost/yii2test/backend/web/index.php?r=user

Access is the user module


Prompt you

This is the view content for action "index". The action belongs to the controller "Backend\modules\user\controllers\defaultcontroller" in the "User" module.

The May customize this page by editing the following file:

D:\WAMP\www\yii2test\backend\modules\user\views\default\index.php

This interface content is accessed by the module user under the Defaultcontroller execution is the index this action.

You may need to customize this page by modifying the views file under the module index.php

Generation of models

The generation of the model first has to configure the database configuration file, the path: yii2test/common/config/main-local.php.


With the database configured, we use model generator to generate the corresponding models.


YII2 will read the tables in the database if there is no user in the database. is unable to read the database field to generate code automatically.

Note here to fill in the namespace, with the path specified to the models under the module user.

Suggest check Enable i18n, can internationalization webapp. Click Generate to generate code.

Crud Method Generation

Here I am using the plugin yii2-kartikgii. About installation and use of yii2-kartikgii reference to the "Yii2 plugin kartikgii"

The following is used to generate the Crud method using the Kartik crud generator

Note that the model class, Search model class, Controller class all need to configure the path specified to the corresponding module; the model ID is user.

Click Generator to generate 8 optional files. The main is to generate Usercontroler and view files.

Through the three steps above. We inadvertently completed the MVC. So let's take a look at the information that the auto-generated code shows us.

Browser input Address

Http://localhost/yii2test/backend/web/index.php?r=user/user

This page accesses the User data table model Crud method under module user. The 5 fields in the table are displayed by default at code generation. The Kartikgii plugin is used. The interface generated by the comparison framework's own Crud method is relatively beautiful. Put the form under the Bootstrap-panel tag. And added features such as export tables, reset tables, and more.

So far we have simply configured the following config file without writing a single line of code. It realizes the operation function of the WebApp to the data table.

Yii Framework is not very powerful, very useful. As for how we operate on the table, we can completely customize it. The following can be put into code writing.

The YII framework has helped us reduce the amount of code development effort. It can be called an efficient PHP development framework.

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.