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
參考:https://www.yii-china.com/post/detail/291.html
https://www.jianshu.com/p/2e2d6142d18c
https://www.php.cn/phpkj/yii/443495.html
https://blog.csdn.net/qq_20757489/article/details/77522617