Gii这个扩展无疑是yii2快速开发的一大助力,通过使用gii生成代码很大程序上节约了开发的时间成本,那么如何使用gii这个组件呢,下边简单介绍一下yii2中gii的一些常用功能
1.首先建一张表
CREATE TABLE `test` (
`id`int(11) NOT NULL AUTO_INCREMENT,
`user` varchar(256) NOT NULL DEFAULT '' COMMENT '姓名',
`age`int(10) NOT NULL DEFAULT '0' COMMENT '年龄',
PRIMARY KEY (`id`),
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='测试表'
2.修改配置
打开config/web.php修改如下配置
if (!YII_ENV_TEST) {
// configuration adjustments for 'dev' environment
$config['bootstrap'][] = 'debug';
$config['modules']['debug'] =[
'class' => 'yii\debug\Module',
'allowedIPs'=>['*']
];
$config['bootstrap'][] = 'gii';
$config['modules']['gii'] =[
'class' => 'yii\gii\Module',
'allowedIPs'=>['*']
];
}
然后在web/index.php下修改,应用应处于开发模式下,按照上面的配置才会打开 Gii 模块
defined('YII_ENV') or define('YII_ENV', 'dev');
3.打开gii页面,打开gii的界面 /index.php?r=gii或者 /gii(url美化之后) ,试试这两种肯定会有一种能打开,http://127.0.0.1/index.php?r=gii 或 http://127.0.0.1/gii
页面打开之后如下
4.生成代码,根据提示可以生成代码了,model,controller等
5.或者用命令行生成,如下
php yii gii/model --ns=app\\models --tableName=test --modelClass=Test
php yii gii/crud --modelClass=app\\models\\Test --controllerClass=app\\controllers\\TestController