Home > Backend Development > PHP Tutorial > Learn the YII2 framework from scratch (5) Use of the rapid code generation tool Gii, yii2gii_PHP tutorial

Learn the YII2 framework from scratch (5) Use of the rapid code generation tool Gii, yii2gii_PHP tutorial

WBOY
Release: 2016-07-13 10:20:36
Original
1435 people have browsed it

Learn the YII2 framework from scratch (5) Use of the rapid code generation tool Gii, yii2gii

Yii2 framework is called a framework for efficient and rapid development because there is a magical tool Gii

Coders who have used the Yii1 framework know that Gii can quickly generate code for you, which means that you may not need to write a single line of code to build a WebApp that can be added, deleted, modified, and checked.

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

Last time I introduced how to install the Yii framework. This time I will introduce how to use the gii tool to quickly implement CRUD functions.

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 more beautiful than Yii1.

The yii2 framework provides 6 generation tools: Model, CRUD, Controller, Form, Module, Extension

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

This time I will use Kartik Crud and the default generation tool to explain.

Module generation

If you want to modularize your application, then Module Generator can help you. Click Module Generator to generate the module

Generate a module named "user"


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

Click Generate to generate code.

After the code generation is completed, you need to configure the file backend/config/main.php and add the module to the application.


Enter the following address into your browser:

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

The user module is accessed


Tips you

This is the view content for action "index". The action belongs to the controller "backendmodulesusercontrollersDefaultController" in the "user" module.

You may customize this page by editing the following file:

D:WAMPwwwyii2testbackendmodulesuserviewsdefaultindex.php

The content of this interface accesses the defaultController under the module user and executes the index action.

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

Model generation

To generate the model, you must first configure the database configuration file, path: yii2test/common/config/main-local.php.


After configuring the database, we use Model Generator to generate the corresponding model.


yii2 will read the table in the database, if there is no user table in the database. It is impossible to read database fields and automatically generate codes.

Please fill in the namespace here, and specify the path with the models under the module user.

It is recommended to check Enable I18N to internationalize WebApp. Click Generate to generate the code.

CRUD method generation

Here I am using the plug-in yii2-kartikgii. Regarding the installation and use of yii2-kartikgii, please refer to "Yii2 Plug-in kartikgii"

Let’s use Kartik CRUD Generator to generate CRUD methodsLearn the YII2 framework from scratch (5) Use of the rapid code generation tool Gii, yii2gii_PHP tutorial

It should be noted that Model Class, Search Model Class, and Controller Class all need to be configured with paths specified to the corresponding modules; the Model ID is user.

Clicking Generator will generate 8 optional files. Mainly to generate UserControler and View files.

Go through the above three steps. We completed MVC inadvertently. So let's see what information the automatically generated code shows us.

Enter the address into the browser

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

Learn the YII2 framework from scratch (5) Use of the rapid code generation tool Gii, yii2gii_PHP tutorial

This page accesses the user data table model CRUD method under the module user. When the code is generated, 5 fields in the table are displayed by default. The Kartikgii plugin is used. Compared with the interface generated by the CRUD method that comes with the framework, the interface is relatively beautiful. Put the table under bootstrap - panel tag. And added functions such as exporting tables and resetting tables.

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

Isn’t the Yii framework very powerful and easy to use? As for how we operate the table, it is completely customizable. You can then start writing code.

Yii framework helps us reduce a lot of code development workload. It can be called an efficient PHP development framework.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/866655.htmlTechArticleLearn the YII2 framework from scratch (5) Use of the rapid code generation tool Gii, yii2gii The reason why the Yii2 framework is called A framework for efficient and rapid development, because there is a magical tool...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template