[yii2] Gii CRUD Template for One Page Ajax

5 October 2021

Home » Blogs » Technology » [yii2] Gii CRUD Template for One Page Ajax

Gii is an extension, which provides a web-based code generator to generate models, forms, modules, CRUDs, and so on.

By default, the following generators are available

  • Generator Model – Generates an ActiveRecord class for a specified database table.
  • CRUD Generator – Generates controllers and displays that implement CRUD operations (Create, Read, Update, Delete) for the specified model.
  • Controller Generator – Generates a new class of controllers with one or more controller actions and a corresponding display.
  • Form Generator – Generates a display script file that displays the form to collect input for the specified model class.
  • Module Generator – Generates the outline code required by the Yii module.
  • Extension Generator – Generates the files required by the Yii extension.
TOP TUTORIALS:  Prevent Windows 10 Specific Updates

By using Gii, writing code becomes faster and easier. When we want to make CRUD operations happen on one page, without switching pages, the default template from Gii can not do that yet.

In this article, we will learn to use Gii templates that already support CRUD operations on one page by using created by johnitvn

Steps to create CRUD in one page with Ajaxcrud

  1. Install the johnitvn/- extension using
php composer.phar require --prefer-dist johnitvn/yii2-ajaxcrud "dev-master"

or adding the following code to .json file

"johnitvn/yii2-ajaxcrud": "dev-master"
  1. This extension also requires Awesome Font,one alternative is to use FontAwesomeAsset from kartik-v/-icons by adding the following code to the .json file.
 "kartik-v/yii2-icons": "*"
  1. Then open Gii with Url: “http://localhost/-advanced/frontend/web/index.php?r=gii”. If all the process goes smoothly, there will be a new menu on Gii, namely “Ajax CRUD Generator”.
TOP TUTORIALS:  How to Flush DNS to clear DNS Cache in Windows 11
ajaxcrud 01
  1. Once everything is ready, we create a model from the database table. In this example we create a model from the “country” table
ajaxcrud 02
  1. After we make the model, the next step is to use CRUD with “Ajax CRUD Generator”
ajaxcrud 03
  1. Then open the with Url: “http://localhost/yii2-advanced/frontend/web/index.php?r=country”. You will see there are “Actions” and “New record” buttons that do not appear.
ajaxcrud 04
  1. To make the button appear, edit “frontend/views/country/index.php” and register “FontAwesomeAsset” from “kartik-v/yii2-icons” as follows.
............
............
use kartikiconsFontAwesomeAsset;
FontAwesomeAsset::register($this);
............
............
  1. Open again Url: “http://localhost/yii2-advanced/frontend/web/index.php?r=country”. You’ll see results like this.
TOP TUTORIALS:  FREE OF CHARGE!!! SSL certificates with Let’s Encrypt and Apache
ajaxcrud 05

Good luck…

May it be useful

0 Comments