为了账号安全,请及时绑定邮箱和手机立即绑定

YII-快速用gii创建一个简单的yii项目!

2018.06.26 19:51 1963浏览

yii提供了强大的命令行工具来快速的创建相关组件和应用。
下面就来讲解用yiic工具快速创建yii应用
我的web目录在 d:\www下 yiiframework在 D:\www\yii\framework
win平台  cd进入 www\yii\framework目录
运行cmd命令


C:\Users******>d:
*【切换到D盘】

D:>cd www\yii\framework

D:\www\yii\framework>yiic webapp../testapp/

*创建一个testapp应用在framework上一级目录【回车】
Create a Web application under 'D:\www\yii\testapp'? [Yes|No]y


*【选择y/yes回车】
mkdir D:/www/yii/testapp
mkdir D:/www/yii/testapp/assets
mkdir D:/www/yii/testapp/css
generate css/bg.gif
generatecss/form.css
generate css/ie.css
generatecss/main.css
generatecss/print.css
generatecss/screen.css
mkdir D:/www/yii/testapp/images
generateindex-test.php
generate index.php
mkdir D:/www/yii/testapp/protected
generateprotected/.htaccess
mkdir D:/www/yii/testapp/protected/commands
mkdir D:/www/yii/testapp/protected/commands/shell
mkdir D:/www/yii/testapp/protected/components
generateprotected/components/Controller.php
generateprotected/components/UserIdentity.php
mkdir D:/www/yii/testapp/protected/config
generateprotected/config/console.php
generateprotected/config/main.php
generateprotected/config/test.php
mkdir D:/www/yii/testapp/protected/controllers
generateprotected/controllers/SiteController.php
mkdir D:/www/yii/testapp/protected/data
generateprotected/data/schema.mysql.sql
generateprotected/data/schema.sqlite.sql
generateprotected/data/testdrive.db
mkdir D:/www/yii/testapp/protected/extensions
mkdir D:/www/yii/testapp/protected/messages
mkdir D:/www/yii/testapp/protected/migrations
mkdir D:/www/yii/testapp/protected/models
generateprotected/models/ContactForm.php
generateprotected/models/LoginForm.php
mkdir D:/www/yii/testapp/protected/runtime
mkdir D:/www/yii/testapp/protected/tests
generateprotected/tests/bootstrap.php
mkdir D:/www/yii/testapp/protected/tests/fixtures
mkdir D:/www/yii/testapp/protected/tests/functional
generateprotected/tests/functional/SiteTest.php
generateprotected/tests/phpunit.xml
mkdir D:/www/yii/testapp/protected/tests/report
mkdir D:/www/yii/testapp/protected/tests/unit
generateprotected/tests/WebTestCase.php
mkdir D:/www/yii/testapp/protected/views
mkdir D:/www/yii/testapp/protected/views/layouts
generateprotected/views/layouts/column1.php
generateprotected/views/layouts/column2.php
generateprotected/views/layouts/main.php
mkdir D:/www/yii/testapp/protected/views/site
generateprotected/views/site/contact.php
generateprotected/views/site/error.php
generateprotected/views/site/index.php
generateprotected/views/site/login.php
mkdir D:/www/yii/testapp/protected/views/site/pages
generateprotected/views/site/pages/about.php
generateprotected/yiic
generateprotected/yiic.bat
generateprotected/yiic.php
mkdir D:/www/yii/testapp/themes
mkdir D:/www/yii/testapp/themes/classic
mkdir D:/www/yii/testapp/themes/classic/views
generatethemes/classic/views/.htaccess
mkdir D:/www/yii/testapp/themes/classic/views/layouts
mkdir D:/www/yii/testapp/themes/classic/views/site
mkdir D:/www/yii/testapp/themes/classic/views/system

Your application has been created successfullyunder

ok 到这里yii新应用已经创建成功

用win+wamp的的朋友可能会遇到下面的问题:
![image.png](http://upload-images.jianshu.io/upload_images/2255249-045f697168f430b2.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)在执行yiic webapp命令时,就报以下错误了: ‘"php.exe"’不是内部或外部命令,也不是可运行的程序或批处理文件。

这个事yiic批处理程序找不到php.exe引起的 
方法一、修改yiic.bat文件
打开yii安装目录d:/www/yii/framework/yiic.bat,yiic.bat有记事本打开有如下一行:  
if "%PHP_COMMAND%" == "" set PHP_COMMAND=php.exe
 
由于我的WAMP安装时没有将php.exe加入到Windows环境变量中,难怪yiic找不到。这样也只需要将php.exe的绝对路径赋给PHP_COMMAND即可。如在我这环境下是这样设置的:  
if "%PHP_COMMAND%" == "" setPHP_COMMAND=D:\wamp\php\php.exe 
 
方法二、将php和yii framework 加入环境变量

在我的电脑右键->属性 
 ![image.png](http://upload-images.jianshu.io/upload_images/2255249-f26bace3a994420a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)![image.png](http://upload-images.jianshu.io/upload_images/2255249-93257473e993d374.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)![image.png](http://upload-images.jianshu.io/upload_images/2255249-9cb7127270e240c2.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)![image.png](http://upload-images.jianshu.io/upload_images/2255249-d006652d874ea4c5.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)确定

完成

*各自根据自己php.exe所在目录设置。




ok yiic创建应用到此结束!~
gii的用法:
首先config配置文件开启gii模块,设置密码。选择数据库,开启连接,配置完毕,建库建表。通过路由访问gii模块,Model Generator 输入*号,自动生成连接的数据库下所有表的模型操作类。
  把刚生成的model类通过 Crud Generator 生成controller和view下的文件。
如果不写表前缀table prefix 生成的model文件,会有表前缀。填上之后,生成的model class会匹配掉表前缀,生成简化的模型名称。table name 一定要写表前缀,否则扫描不到数据表。
![Paste_Image.png](http://upload-images.jianshu.io/upload_images/2255249-96c957bc4d2b37b5.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)Modules模块生成demo:输入APP,会在protected文件夹生成modeules/APP文件夹。
模块生成后,用gii配置,需要在main.php 文件 modules添加如下代码:
'modules'=>array(    // uncomment the following to enable the Gii tool
     

 
    'gii'=>array(        'class'=>'system.gii.GiiModule',        'password'=>'a',        // If removed, Gii defaults to localhost only. Edit carefully to taste.
        'ipFilters'=>array('127.0.0.1','::1'),
    ),    'Home'=>array(        'class'=>'application.modules.Home.HomeModule',
    ),     
    'Admin'=>array(        'class'=>'application.modules.Admin.AdminModule',
    ),     
    'App'=>array(        'class'=>'application.modules.App.AppModule',
    ),
     
),
![Paste_Image.png](http://upload-images.jianshu.io/upload_images/2255249-b87fe13354bf9c67.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

![Paste_Image.png](http://upload-images.jianshu.io/upload_images/2255249-b2401797c5193a05.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
生成的模块在项目下protected/modules下。model模型公用。curd生成时,model class可以和前面相同,但controller id 要指定module路径。![Paste_Image.png](http://upload-images.jianshu.io/upload_images/2255249-0fb2e777e86ea2aa.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
然后在前台只设读的权限(在控制器文件中把 public $layout='//layouts/column2'; column2改成column1),
调用布局文件(protected\views\layouts\column1.php)



作者:DragonersLi
链接:https://www.jianshu.com/p/3b224eba1a27

点击查看更多内容
Yii
0人点赞

若觉得本文不错,就分享一下吧!

评论

相关文章推荐

正在加载中
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消