yiic <command-name> [parameters...]
command-name对应的有
message 搜索指定文件信息 yicc message webroot/xxx.php
shell 进入命令行模式
webapp 创建项目
migrate 数据迁移计划
如果要查看具体的命令的功能可以用
yiic help <command-name>
1.yiic webapp 建立项目
执行 yiic help webapp
/*
USAGE
yiic webapp <app-path>
DESCRIPTION
在指定的位置创建一个基于yii的完整结构的webapp。app-path是webapp的存放目录。如果目录不存在会被创建。
PARAMETERS
* app-path: required, the directory where the new application will be created.
If the directory does not exist, it will be created. After the application
is created, please make sure the directory can be accessed by Web users.
*/
2.yiic messag
yiic help message打印message命令的相关用法
主要用于webapp的国际化。需要配置文件。yii为我们提供了配置文件的模板。
framework/messages/config.php
复制framework\messages\config.php 文件到 protected\messages\下
具体内容修改如下:
<config-file><pre name="code" class="php"><?php
/**
* This is the configuration for generating message translations
* for the Yii framework. It is used by the 'yiic message' command.
*/
return array(
'sourcePath'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..',
'messagePath'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'messages',
'languages'=>array('zh_cn'),
'fileTypes'=>array('php'),
'overwrite'=>true,
'exclude'=>array(
'.svn',
'yiilite.php',
'yiit.php',
'/i18n/data',
'/messages',
'/vendors',
'/web/js',
),
);
</pre><br>
<br>
<pre></pre>
<p></p>
<p><span style="font-size:10px">/www/yii_dev/testwebap/protected/views/site/login.php<br>
</span></p>
<p><span style="font-size:10px"> 修改如下</span></p>
<p><span style="font-size:10px"><?php echo $form->labelEx($model,<strong>Yii::t('login_message','username')</strong>); ?><br>
<span style="white-space:pre"></span><?php echo $form->textField($model,'username'); ?><br>
<span style="white-space:pre"></span><?php echo $form->error($model,'username'); ?><br>
</span></p>
<p><span style="font-size:10px"><br>
</span></p>
<p><span style="font-size:10px"><?php echo $form->labelEx($model,<strong>Yii::t('login_message','password')</strong>); ?><br>
<span style="white-space:pre"></span><?php echo $form->passwordField($model,'password'); ?><br>
<span style="white-space:pre"></span><?php echo $form->error($model,'password'); ?><br>
</span></p>
<p><span style="font-size:10px">执行如下命令</span></p>
<p><span style="font-size:10px">testwebap/protected/messages<br>
</span></p>
<p><span style="font-size:10px">/www/yii_dev/yii/framework# php yiic message "../../testwebap/protected/messages/config.php"<br>
Extracting messages from /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/protected/components/Controller.php...<br>
Extracting messages from /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/protected/components/UserIdentity.php...<br>
Extracting messages from /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/protected/config/console.php...<br>
Extracting messages from /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/protected/config/main.php...<br>
Extracting messages from /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/protected/config/test.php...<br>
Extracting messages from /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/protected/controllers/SiteController.php...<br>
Extracting messages from /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/protected/models/ContactForm.php...<br>
Extracting messages from /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/protected/models/LoginForm.php...<br>
Extracting messages from /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/protected/tests/WebTestCase.php...<br>
Extracting messages from /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/protected/tests/bootstrap.php...<br>
Extracting messages from /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/protected/tests/functional/SiteTest.php...<br>
Extracting messages from /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/protected/views/layouts/column1.php...<br>
Extracting messages from /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/protected/views/layouts/column2.php...<br>
Extracting messages from /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/protected/views/layouts/main.php...<br>
Extracting messages from /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/protected/views/site/contact.php...<br>
Extracting messages from /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/protected/views/site/error.php...<br>
Extracting messages from /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/protected/views/site/index.php...<br>
Extracting messages from /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/protected/views/site/login.php...<br>
Extracting messages from /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/protected/views/site/pages/about.php...<br>
Extracting messages from /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/protected/yiic.php...<br>
Saving messages to /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/protected/messages/../messages/zh_cn/login_message.php...saved.<br>
</span></p>
<p><span style="font-size:10px">目录下生成如下文件</span></p>
<p><span style="font-size:10px">/www/yii_dev/testwebap/protected/messages/zh_cn/login_message.php<br>
</span></p>
<p><span style="font-size:10px">return array (<br>
'password' => '',<br>
'username' => '',<br>
);<br>
</span></p>
<p><span style="font-size:10px">改为</span></p>
<p><span style="font-size:10px">return array (<br>
'password' => '密码',<br>
'username' => '<span style="font-size:10px">用户名</span>',<br>
);<br>
</span></p>
<p><span style="font-size:10px">修改</span></p>
<p><span style="font-size:10px">如下配置文件</span></p>
<p><span style="font-size:10px">/www/yii_dev/testwebap/protected/config/main.php<br>
</span></p>
<p><span style="font-family:Georgia; font-size:10px; color:#333333"><span style="letter-spacing:2px; line-height:20px">添加如下</span></span></p>
<p><span style="font-family:Georgia; font-size:10px; color:#333333"><span style="letter-spacing:2px; line-height:20px"><span style="white-space:pre"></span>// application-level parameters that can be accessed<br>
<span style="white-space:pre"></span>// using Yii::app()->params['paramName']<br>
<span style="white-space:pre"></span>'params'=>array(<br>
<span style="white-space:pre"></span>// this is used in contact page<br>
<span style="white-space:pre"></span>'adminEmail'=>'webmaster@example.com',<br>
<span style="white-space:pre"></span>),<br>
<strong><span style="white-space:pre"></span>'language'=>'zh_cn',<br>
<span style="white-space:pre"></span>'sourceLanguage'=>'en_us',</strong><br>
);<br>
</span></span></p>
<p><span style="font-family:Georgia; font-size:10px; color:#333333"><span style="letter-spacing:2px; line-height:20px">访问一一下</span></span></p>
<p><span style="font-family:Georgia; font-size:10px; color:#333333"><span style="letter-spacing:2px; line-height:20px"><a href="http://www.localyii.com/testwebap/index.php?r=site/login">http://www.localyii.com/testwebap/index.php?r=site/login</a><br>
</span></span></p>
<p><span style="font-family:Georgia; font-size:10px; color:#333333">可以看到翻译成中文了。</span></p>
<p><span style="font-family:Georgia; font-size:10px; color:#333333">可以有乱码的问题,注意编码问题。文件,浏览器要设置统一</span></p>
<p><span style="font-size:24px"><strong><br>
</strong></span></p>
<p><span style="font-size:24px"><strong>明天讲</strong></span></p>
<p><span style="font-size:24px"><strong></strong></span></p>
<p><strong><span style="font-size:24px">3.php yiic migrate</span></strong></p>
<p><strong><span style="font-size:24px">4.php yiic shell</span></strong></p>
<br>
<p> </p>
<p> </p>
</config-file>
3.yiic SHELL
yiic help shell打印sell命令的相关用法
controller 创建控制器
crud 根据模型创建对应的控制器
form 根据模型创建表单视图
help 帮助命令
model 根据表名创建模型
moduel 创建组件
创建命令 总结: 流程先建立数据库,根据表结构创建对应的model(model命令),再根据model创建对应的控制器和视图(crud命令)。
(1)、controller 控制器
controller message helloworld goodbye
message 类前缀,文件前缀,后面可以加无数方法 但是如果以后增加或者修改方法用命令比较麻烦,建议以后手动加。
(2)、crud根据模型创建对应的控制器,必须先建立数据表,才能根本表和模型创建对应的控制器
Crud crud modelClassName controllerName
modelClassName 模型类前缀,文件前缀 controllerName 控制器类前缀,文件前缀
用http模式输入r=gii 如果有没模型文件无法创建,但命令行模式不受影响
(3)、form根据模型创建对应的控制器,必须先建立数据表,才能根本表和模型创建对应的控制器。
Form modelClassName ViewName ViewPath
modelClassName 模型类前缀,文件前缀 ViewName 视图文件
ViewPath 视图路径 application.views或者自己组件的地址
(4)、model根据模型创建对应的控制器,必须先建立数据表,才能根本表和模型创建对应的控制器。
model modelClassName tableName
modelClassName 要创建的模式,模型类前缀,文件前缀 tableName 表名
(5)、module 创建模块
module moduleId
moduleId 模块ID
4.yiic migrate
所有的迁移命令后面可以加上一些参数
--interactive=<true|false> 是否使用交互模式
--migrationPath=app.migrations设置迁移类所在的文件夹
--migrationTable=tbl_migration 设置迁移状态记录表
--connectionID=db设置要将迁移应用到哪个数据库
--templateFile=application.migrations.template设置生成迁移类的模板文件
1)、创建迁移目录:
cd protected
mkdir migrations
migrations 是数据库临时目录,这个必须先手动创建
2)、创建新的迁移:
Yiic migrate create <name>
创建一个新的迁移计划,产生一个类在protected/migrations下,你需要实现up和down两个函数
Up $this->createTable(‘表名’, array(结构));
Down $this->dropTable(‘表名’);删除该表
3)、应用所有迁移:
Yiic migrate
如果没有给出“行动”参数,则默认为“向上”。
4)、向上迁移:
Yiic migrate up 3
应用三步迁移,若没有数字默认是1
5)、还原迁移:
Yiic migrate down 3
还原最后3步应用迁移,若没有数字默认是1
6)、指定版本迁移:
Yiic migrate to 120808_125201
将迁移应用至2012-08-08 12:52:01创建的那个迁移计划
7)、标记指定迁移版本:
Yiic migrate mark 120808_125201
让系统认为现在的迁移是2012-08-08 12:52:01的那个,但是不实际执行迁移计划。
这点与to命令不同,to会执行迁移计划应用,然后将状态标记,这个只标记。
8)、查看已应用迁移:
Yiic migrate history [limit]
查看已应用的limit步迁移,若没有limit则显示所有
9) 查看可应用迁移:
Yiic migrate new [limit]
查看可以应用的limit步迁移,若没有limit则显示所有
yiic提供了创建yii应用的各种方法。有时候可能需要自己定义一个命令来提高开发效率。
控制台的命令配置文件是应用的protected/config/console.php文件,例如我们想使用数据库
<?php
// This is the configuration for yiic console application.
// Any writable CConsoleApplication properties can be configured here.
return array(
'basePath'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..',
'name'=>'My Console Application',
// application components
'components'=>array(
'db'=>array(
'connectionString' => 'sqlite:'.dirname(__FILE__).'/../data/testdrive.db',
),
// uncomment the following to use a MySQL database
/*
'db'=>array(
'connectionString' => 'mysql:host=localhost;dbname=testdrive',
'emulatePrepare' => true,
'username' => 'root',
'password' => '',
'charset' => 'utf8',
),
*/
),
);
在配置文件中开启相应的数据就可以了。
这里提供简单的例子来讲述如何控制yiic控制台,自定义自己的命令。
关于类文件
存放位置
/protected/commands
类的定义规则
必须集成CConsoleCommand类
例如
TestCommand.php
<?php
class TestCommand extends CConsoleCommand
{
public function getHelp()
{
return '这里显示命令的帮助信息';
}
/**
* Execute the action.
* @param array command line parameters specific for this command
*/
public function run($args)
{
if(!isset($args[0]))
$this->usageError('请输入参数.');
echo('你输入的参数是 :\n');
var_dump($args);
}
}
/www/yii_dev/testwebap/protected/ yiic test p1 p2 p3
你输入的参数是 :\narray(3) {
[0]=>
string(2) "p1"
[1]=>
string(2) "p2"
[2]=>
string(2) "p3"
}
框架中的代码就是最好的例子,如果要写控制台命令,可以仔细研读。
http://www.yiiframework.com/doc/guide/1.1/zh_cn/topics.console