Learn YII2 framework from scratch (v) Quick generation of code tools using the Gii, yii2gii_php tutorial

Source: Internet
Author: User
Tags yii

Starting from scratch YII2 Framework (v) Rapid generation of code tools using the Gii, yii2gii


The YII2 framework is called a framework for efficient and rapid development because there is a magical tool for the GII

With the coder of the YII1 framework, we know that the GII can generate code quickly for you, which means that you can create a webapp that could be deleted and changed without having to write a single line of code.

Of course, as a coder, how can we achieve the function we want without writing code?

The last time we introduced how to install the YII framework, this article describes how to use the GII tool to quickly implement CRUD functionality.

Once the framework is installed, you can access the GII tools by following these links

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

YII2 's GII tool uses bootstrap, and the contrast Yii1 looks a lot prettier.

The YII2 Framework provides a build tool with 6: model,crud,controller,form,module,extension

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

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

Generation of modules

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

Generate a module named "User"


Click on Preview to see the GII will help us generate three files and click the link to preview the code.

Click Generate to generate the code.

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


The browser enters the following address:

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

The user module is accessed


Prompts you

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

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 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, path: yii2test/common/config/main-local.php.


To configure the database, we use model generator to generate the corresponding models.


YII2 will read the table in the database if there is no user in the database. Is that the database fields cannot be read automatically generated code.

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

It is recommended to tick enable i18n, which can be internationalized WebApp. Click Generate to generate the code.

Crud Method Generation

Here I am using the plugin yii2-kartikgii. Reference to the installation and use of the yii2-kartikgii "Yii2 plug-in Kartikgii"

Here's how to use the Kartik crud generator to generate a crud method

It is important to note that the model class, the Search model class, and the Controller class require the configuration path to be assigned to the appropriate module, and the model ID is user.

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

Go through the three steps above. We inadvertently completed the MVC. So let's see what 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 when code is generated. The Kartikgii plugin is used. The interface generated by the comparison framework's own Crud method is relatively beautiful. Place the table under the Bootstrap-panel tab. And added features such as export tables, reset tables, and more.

At this point we simply configure the config file, without writing a single line of code. It realizes the operation function of WebApp to the data table.

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

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




http://www.bkjia.com/PHPjc/866655.html www.bkjia.com true http://www.bkjia.com/PHPjc/866655.html techarticle Starting from scratch YII2 Framework (v) Rapid generation of code tools for the use of the Gii, the Yii2gii YII2 framework is called a framework for efficient and rapid development because there is a magical tool ...

  • Related Article

    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.