This website uses cookies

Our website, platform and/or any sub domains use cookies to understand how you use our services, and to improve both your experience and our marketing relevance.

CloudwaysCDN — a powerful solution that offers superior performance and satisfied global audience for your business. Read More

Getting Started With Gii in Yii 2: Creating a To-do List

Updated on  March 2, 2018

5 Min Read
Reading Time: 5 minutes

In the last installment of this series, I covered a simple contact form app powered by Yii2. In this article I will discuss Gii (a Yii2 module) and create a CRUD capable application using Gii.

Creating a to-do list on Gii in Yii2

What is Gii

Gii is a web-based code-generator module provided by Yii framework. It helps you create and generate fully customized forms, models, CRUD for database and more. To access Gii in Yii2, just route your url to index.php?r=gii.

Gii Web Page

To demonstrate the capabilities of Gii, I will create a Model and CRUD for a to-do list using Gii. Let’s get started.

Related: How To Host Yii 2 Framework On Cloudways Using Composer

Creating a Table for To-do list:

Let us first create a new table in the same database that we used in the previous article. This table will be used to save to-do items. Schema for the table is:

In order to create a CRUD for this table, we need to create its model first. Now,  we don’t need to write the code for it; I will be using Gii Model Generator for it. Let’s do it.

Generating Module Using Gii

Now open Gii in the browser and click Start under Model generator.

Start With Model Generator

A form will open in which will ask the table name and model class. In the table name type t and the table name will automatically be suggested with the reference letter.

Table Name

After selecting the table, press Tab and a class with the same name as the table will be automatically added in the text box.

image06

Leave the other options as they are and click preview button. Once you have clicked the preview, you will see what files will be generated. Since we are only making a model class, only one file will be generated.

Click Generate

Once the process finishes,  you will see the message that your code has been generated successfully.

Code Generated

Now head to the model folder and you will see a new model Todo is generated.

Todo.php

Next, let’s create a CRUD for this model.

Generating CRUD using Gii

Now on the page where we created the model, click CRUD Generator.

CRUD Generator

Fill the form that opens.

  • Add the model class for which we are generating CRUD. This requires the full path of the model that must have been generated beforehand. For now, use
    app\models\Todo
  • Next, the model for the search classis required. Since this class has not been generated yet,  just name it and the CRUD generator will generate this class automatically. Let’s call the class TodoSearch.  The path of this model is
    app\models\TodoSearch
  • The controller that will be generated also requires a name. Let’s call this controller TodoController. The path for the controller is
    app\controllers\TodoController
  • Last the view for the app must also be named. The path for the view is
    app\views\todo

CRUD Generator

Now click Preview.

Click Preview

A look of the files that would be generated confirms that in addition to the Controller and the Search model, all the necessary views for the CRUD operations will also be generated. Click Generate  to complete the process.

Generated Files

Now that all the files have been generated, go to the Yii directory and you will find that all the code has already been generated.

Todo.php

Now let us test the newly created app by executing CRUD operations.

Creating And Updating the To-do List

Let’s test the app by going to the controller index: ?r=todo/index.

Create Todo

This page will show a blank list, since we haven’t added any items yet. Let us insert a new item by clicking the  Create Todo button.

Create Todo

Notice that Duedate is text. This needs to be changed to date so that the users could pick a date from the date widget and the app could save the input in the proper format. For this,  go to the todo folder inside the views and open _form.php. Change this line of code:

to

Now save the file and refresh the page. You will see that input type has been change to date.

Change Date

Now let’s add a new to-do event using this form. Once you have added the todo, you will be redirected to it’s view page.

Your First Todo

Let’s say, the item added needs to be updated. Just click the update button and a form will open.

Update Todo

When you want to see the complete list of items, just  go to the main index page, ?r=todo/index.

Todos

If you want to delete an item from the main list, just click the Delete Icon button.

Conclusion:

In this article I discussed Gii and how it works with Yii2. I elaborated by developing a simple CRUD capable app to highlight the working of Gii on Yii2 framework. In the fifth part of this series, I will cover Redis Configuration in Yii2. Here is the complete list of topics covered in this series on Yii2.

Share your opinion in the comment section. COMMENT NOW

Create PHP websites without the worry of server management.

Deploy your PHP app on optimized PHP hosting servers.

About The Author

Ahmed Khan

Ahmed was a PHP community expert at Cloudways - A Managed PHP Hosting Cloud Platform. He is a software engineer with extensive knowledge in PHP and SEO. He loves watching Game of Thrones is his free time. Follow Ahmed on Twitter to stay updated with his works. You can email him at ahmed.khan@cloudways.com

Stay Connected:

Get Our Newsletter
Be the first to get the latest updates and tutorials.

THERE’S MORE TO READ.