Gii is a Yii the module . You can configure the application's modules turn it attributes. Generally in the config/web.php
file will have the following configuration code:
$config = [ ... ];
if (YII_ENV_DEV) {
$config['bootstrap'][] = 'gii'; $config['modules']['gii'] = [ 'class' => 'yii\gii\Module', ]; }
This configuration indicates that, if the current is the development environment , the application will include gii
modules, classes are yii \ gii \ Module.
If you check the application entry script web/index.php
, you'll see this line of code YII_ENV_DEV
is set to true:
defined('YII_ENV') or define('YII_ENV', 'dev');
Given the definition of these lines of code, application development mode in accordance with the above configuration will open Gii module. You can access Gii directly through the URL:
http://hostname/index.php?r=gii
Information: If you access Gii via a machine other than this machine, the request will be denied for security reasons. You can configure to allow access Gii to add the IP address:
'gii' => [ 'class' => 'yii\gii\Module', 'allowedIPs' => ['127.0.0.1', '::1', '192.168.0.*', '192.168.178.20'] // 按需调整这里,如何设置*.*.*.*表示所有ip均可以访问,设置这个肯能存在安全风险,记得及时删除 ],
Generate active record class
Select "Model Generator" (click on the link Gii Home) to generate the active record class. And fill out a form like this:
- Table Name:
country
- Model Class:
Country
Then click "Preview" button. You will see models/Country.php
are listed in the list of files that will be generated. Click the file name can preview content.
The same file if you have already created, use Gii will overwrite it, click next to the file name diff
can view the contents of the difference between the existing file with the file that will be generated.
Want to overwrite the existing file, select the check box under the "overwrite" and then click on the "Generator". If a new file, just click on the "Generator" is like.
Next you will see a description page that contains the generated files. If the build process been overwritten file, there will be a message stating the code is regenerated coverage.
Code generation CRUD
On behalf of CRUD add, search, change, delete operation, which is commonly used in data processing vast majority of Web sites. Select Gii in "CRUD Generator" (click on the link Gii home page) to create CRUD functionality. In this example "country" needs to fill out a form like this:
- Model Class:
app\models\Country
- Search Model Class:
app\models\CountrySearch
- Controller Class:
app\controllers\CountryController
Then click "Preview" button. You will see the following list of files that will be generated.
If you had created before controllers/CountryController.php
and views/country/index.php
files (using the database section Guide), select the check box under the "overwrite" overwrite them (before the file could not support all CRUD).
Commissioning
Visit the following URL to view the browser to run the generated code:
http://hostname/index.php?r=country/index
You can see a grid display with the national data read from the data table. Support sort the data in the column header, enter the filter criteria for screening.
Details can browse, edit, or delete grid of each country. You can also click on the top of the grid "Create Country" button to create a new nation through the form.
The following lists the files generated by the Gii, in order to study the function and you realize, or modify them.
- Controller:
controllers/CountryController.php
- Model:
models/Country.php
andmodels/CountrySearch.php
- view:
views/country/*.php