YII2 framework (5) quick code generation tool Gii from scratch, yii2gii

Source: Internet
Author: User

YII2 framework (5) quick code generation tool Gii from scratch, yii2gii

The Yii2 framework is called a framework for efficient and rapid development, because there is a magic tool Gii

Coder that has used the Yii1 framework knows that Gii can quickly generate code for you, that is, building a WebApp that can add, delete, modify, and query data may not need to write a line of code.

Of course, as a Coder, how can we implement the functions we want without writing code.

The last time I introduced how to install the Yii framework, this time I introduced how to use the gii tool to quickly implement the CRUD function.

After the framework is installed, you can access the Gii tool through the following link:

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

The Gii tool of Yii2 uses Bootstrap, which looks much better than Yii1.

The yii2 framework provides six generation tools: Model, CRUD, Controller, Form, Module, and Extension.

I installed a yii2-kartikgii plug-in that is used to generate the CRUD method in Kartik mode.

This section describes how to use Kartik Crud with the default generation tool.

Module generation

If you want to implement application modularization, Module Generator can help you. Click Module Generator to generate a Module.

Generate a module named "user"

Click Preview to see that Gii will help us generate three files. You can click the link to Preview the code.

Click Generate to Generate the code.

After the code is generated, configure the backend/config/main. php file and add the module to the application.

Enter the following address in the browser:

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

The user module is accessed.

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.

You may customize this page by editing the following file:

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

In this interface, the defaultController under the module user executes the index action.

You may need to customize this page by modifying the View File index. php under the module

Model generation

First, you have to configure the database configuration file, path: yii2test/common/config/main-local.php.

After the database is configured, we use Model Generator to generate the corresponding Model.

Yii2 will read the table in the database, if there is no user in the database. The database fields cannot be read to automatically generate code.

Enter the namespace. Specify the path to models under the Module user.

We recommend that you select Enable I18N to internationalize the WebApp. Click Generate to Generate the code.

CRUD method generation

Here I am using a plug-in yii2-kartikgii. For the installation and use of yii2-kartikgii, see Yii2 plug-in kartikgii

The following uses Kartik CRUD Generator to generate the CRUD method:

You must specify the path for Model Class, Search Model Class, and Controller Class to the corresponding module. The Model ID is user.

Click Generator to generate eight optional files. Generate the UserControler and View files.

Go through the above three steps. We inadvertently completed MVC. Then let's take a look at what information the automatically generated code shows for us.

Enter the address in the browser

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

This page accesses the CRUD method of the user data table model under the Module user. By default, five fields in the table are displayed when the code is generated. The Kartikgii plug-in is used. Compared with the built-in CRUD method, the interface generated by the Framework is relatively more beautiful. Place the table under the bootstrap-panel tab. You can also export tables and reset tables.

At this point, we have simply configured the config file and did not write a line of code. The WebApp can operate data tables.

Is the Yii framework very powerful and useful. You can define how to operate tables. You can write the code later.

The Yii framework reduces the workload of code development. 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.