A deep dive into Gii (opens new window)
Work In Progress
Obviously screenshots cannot show everything Gii can do. However we try to show the most important features. Also, here are some related links:
The technology behind the mDIS Template manager
Gii is the Yii code-generation extension.
Internally, the mDIS Templates-Manager uses Gii (opens new window) quite a bit, therefore it is useful, for comparison, to show here the "native" GUI of Gii, as included in Yii2, and intended for code generation.
Gii and Symfony Maker Bundle
Yii2 includes a powerful code generation tool called Gii (opens new window) that allows generating various code snippets and entire modules through a web interface or command line. Experienced PHP developers might know that Gii is quite similar to Symfony's "Maker Bundle" (opens new window) in functionality.
Gii is included in Yii2 but needs to be enabled. To enable it, add the following to backend/config/{console,web}.php
:
'bootstrap' => ['gii'],
'modules' => [
'gii' => [
'class' => 'yii\gii\Module',
],
]
2
3
4
5
6
> # Output when running ./yii gii on the console
> ./yii gii
SUB-COMMANDS
- gii/controller Controller Generator
- gii/crud CRUD Generator
- gii/extension Extension Generator
- gii/form Form Generator
- gii/index (default)
- gii/model Model Generator
- gii/module Module Generator
To see the detailed information about individual sub-commands, enter:
yii help <sub-command>
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
(There are also the ./yii cg
commands, see below.)
For "normal" usage of GII, mDIS developers should set up a new basic Yii project.
A basic Yii project template can be created with command
composer create-project --prefer-dist --stability dev yiisoft/yii2-app-basic basic
.
You can then access the web interface at /index.php?r=gii
or at the /gii
route to see what is displayed below.
Only do this on a development workstation.
Preview
For illustration purposes only
The content of the screenshots shown below is not part of mDIS.
It is shown here to demonstrate the "default" look and feel of the Gii code-generator which mDIS uses internally. The mDIS Version of Gii is accessed via its PHP APIs. The output is then heavily customized.
You can see that the Preview feature of the Gii GUI is somewhat similar to the mDIS Templates-Manager Preview:
Note the similarities with the mDIS Template manager. The look of the "Preview" and "Generate" buttons should look familiar. So should the workflow of with the "Create/Overwrite" alternatives.
What's the magic behind the code generation process?
Background on Gii's graphical user interface
Gii has a graphical user interface that is shown here step by step, to reduce complexity.
By default the sidebar looks like this:
Sidebar of Gii
As you can see from the sidebar, there are 5 categories of code generators available.
Key Message
Internally, the mDIS Templates-Manager uses 2 generator classes:
- the Base Generator (in file
backend/modules/cg/generators/DISForm/Generator.php
) - the Model Generator (in file
backend/modules/cg/generators/DISModel/Generator.php
)
Here we only show what these features look like, not how they can be used. That would be out of the scope of this article.
Base Generator
PHP class Generator
in file backend/modules/cg/generators/DISForm/Generator.php
inherits
from \yii\gii\Generator
. This is the base class for all generator classes.
A generator instance is responsible for taking user inputs, validating them, and using them to generate the corresponding code based on a set of code template files. A generator class typically needs to implement the following methods:
getName()
: returns the name of the generatorgetDescription()
: returns the detailed description of the generatorgenerate()
: generates the code based on the current user input and the specified code template files. This is the place where main code generation code resides.
Model Generator GUI
The default Graphical User Interface of the Model Code-Generator has a lot of helpful tooltips (dashed lines).
This generator generates an ActiveRecord class for the specified database table.
[TBC]
The PHP class in file in file backend/modules/cg/generators/DISModel/Generator.php
inherits from \yii\gii\generators\model\Generator
.
Illustration purposes only
mDIS does not use the default Gii CRUD generator and Gii Form generators shown below, because they generate code based on the Twitter Bootstrap UI framework plus some Yii-specific JavaScript code. mDIS does not use this framework for the front end. mDIS uses VueJS/Vuetify instead. We still include this here, because what the mDIS code generator accomplishes is conceptually similar.
CRUD Generator GUI
The default Graphical User Interface of the CRUD Code-Generator. (not used by mDIS)
This generator generates a controller and views that implement CRUD (Create, Read, Update, Delete) operations for the specified data model.
[TBC]
Form Generator GUI
The default Graphical User Interface of the Form Code-Generator. (not used by mDIS)
This generator generates a view script file that displays a form to collect input for the specified model class.
[TBC]
The code generation process in Yii is implemented according to the MVC composite pattern.
cg on the mDIS yii console
There is a yii cg
console runner task. It seems to be unfinished work, though
See also the output of ./yii
which will show this:
The following commands are available:
...
- cg This is the command line version of Gii - a code generator.
cg/dis-form DIS Form Generator
cg/index (default)
2
3
4
5
6
At this time (2019) this results:
./yii cg dis-form
Error: No help for unknown command "gii"
2
3
4
mDIS cg API
TBC
(Back to developer page)