Automatically Generate Code for Model, View and Controller using Gii: Yii2 Framework
Image Source
Repository
https://github.com/vegasteem/Gii-tutorial
What Will I Learn?
A lot of approaches are available to create application in Yii2 framework manually. In this tutorial we will learn to create an Electronic appliances web application automatically with features to Create, View, Update and Delete the entries of a MySQL database table. Yii2 Framework is based on MVC so in other words we will learn to create Model(M), View(V) and Controller(C) code automatically using a user interface provided by Yii2. The user interface is knows as Gii.
Requirements
- Beginner Knowledge About PHP and Object Oriented Programming
- Beginner Knowledge About MySQL
- Beginner Knowlede About Using phpMyAdmin to manage MySQL database
- Xampp/ Wamp Server for running Apache, PHP, MySQL and phpMyAdmin
Difficulty
- Intermediate
Tutorial Contents
Step 1: Create a Yii2 Framework project in your web accessible folder.
Step 2: Start the Xampp/ Wamp Server either from command line or using their GUI.
Step 3: Open phpMyAdmin and create a database in MySQL named "electronics".
After creating the database run the following query to create a table inside database:
CREATE TABLE IF NOT EXISTS appliances
(
id
int(11) NOT NULL AUTO_INCREMENT,
name
varchar(30) NOT NULL,
quantity
int(11) NOT NULL,
category
varchar(30) NOT NULL,
PRIMARY KEY (id
) USING BTREE,
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=11 ;
Run the following query to insert values inside database:
INSERT INTO `appliances` (`id`, `name`, `quantity`, `category`) VALUES ('1', 'vegasteem1', '20', 'vegasteem_category1'), ('2', 'vegasteem2', '30', 'vegasteem_category3');
Step 4: Getting started with Gii
To enable Gii in your project open config/web.php file in your which has the following contents:
config = [ ... ];
if (YII_ENV_DEV) {
$config['bootstrap'][] = 'gii';
$config['modules']['gii'] = [
'class' => 'yii\gii\Module',
];
}
- 'class' field in above code is set to 'yii\gii\Module' which tells the path where the Gii module
We can now access Gii via the following URL:
http://localhost/advaced/backend/web/index.php?r=gii
Here "advaced" is my yii2 project name.
Step 5: Generating Model for the project
You can access the Model Generator through this link:
http://localhost/advaced/backend/web/index.php?r=gii%2Fdefault%2Fview&id=model
We just have to enter values in 'Table Name' and 'Model Name' field, keep default values in rest of the fields. Then click Preview, you will see models/Appliances.php to be listed. Click on Generate button to create the model file.
Step 6: Generating Controller and View for the project
You can access the CRUD Generator through this link:
http://localhost/advaced/backend/web/index.php?r=gii%2Fdefault%2Fview&id=crud
Enter the values in below fields as:
- Model Class: backend\models\Appliances
- Search Model Class: backend\models\AppliancesSearch
- Controller Class: backend\controllers\AppliancesController
- View Path: @backend/views/appliances
Then click Preview, you will see the controller and view files to be listed. Click on Generate button to create all those files.
Step 7: Viewing the Read, Update and Delete user interface.
An simple and elegant user interface is created that lists the entries of our MySQL database in a grid form.
You can access this file using:
http://localhost/advaced/backend/web/index.php?r=appliances/index
The 3 options on the right hand side of every record in list represents View, Update and Delete respectively.
It can be seen that the entries listed in above image are same as the values inserted by us using the insert command give n above in this article.
The form to create an entry for database table can be accessed using:
http://localhost/advaced/backend/web/index.php?r=appliances%2Fcreate
Thus an elegant, efficient and simple web application with Create, Read, Update and Delete functionality can be constructed automatically, without much knowledge of coding and in very less time.
Proof of work done
https://github.com/vegasteem/Gii-tutorial
All the images included in the article are in sync with the values provided in the code of this article.
Thank you for your contribution.
Need help? Write a ticket on https://support.utopian.io/.
Chat with us on Discord.
[utopian-moderator]
Congratulations @vegasteem! You have completed some achievement on Steemit and have been rewarded with new badge(s) :
Award for the number of upvotes received
Click on the badge to view your Board of Honor.
If you no longer want to receive notifications, reply to this comment with the word
STOP
To support your work, I also upvoted your post!
Congratulations @vegasteem! You have completed some achievement on Steemit and have been rewarded with new badge(s) :
You got your First payout
Click on the badge to view your Board of Honor.
If you no longer want to receive notifications, reply to this comment with the word
STOP
Do not miss the last post from @steemitboard!
Participate in the SteemitBoard World Cup Contest!
Collect World Cup badges and win free SBD
Support the Gold Sponsors of the contest: @good-karma and @lukestokes