可以用来做定时程序通过命令运行代码,非url访问方式更安全
linux/macos 上使用yiic配置(window上使用yiic.bat)
1.yiic要有执行权限,chmod -R 777 yiic
2.yiic用的PHP,需要指定PHP的正确路径:
#!/usr/bin/env php会自动的在你的用户PATH变量中所定义的目录中寻找php来执行的。
用vi打开yiic,把“#!/usr/bin/env php” 改为“#!/usr/local/php/bin/php(也就是你php路径)
也可以>/usr/local/bin/php yiic
1.yiic 命令用到的是yiic.php,也可以直接用yiic.php
2.控制台的命令配置文件是应用的protected/config/console.php文件,
<?php
define('ROOT_PATH' , "/Users/apple/development/pyq_tuiguang/");
$yii = '/sites/www/yii/framework/yiic.php';
$config = ROOT_PATH . 'protected/config/console.php';
require_once($yii);
Yii::createWebApplication($config)->run();
系统默认的路径是protected/commands/shell 如果你执行单一的任务,直接在run方法里面写,另外一种就是同写你的Controller(控制器),前面增加actionXXX
protected/extensions/clean_command/ECleanCommand.php
<?php
class ECleanCommand extends CConsoleCommand
{
public $webRoot = null;
public function getHelp()
{
$out = "Clean command allows you to clean up various temporary data Yii and an application are generating.\n\n";
return $out.parent::getHelp();
}
public function actionCache()
{
$cache=Yii::app()->getComponent('cache');
if($cache!==null){
$cache->flush();
echo "Done.\n";
}
else {
echo "Please configure cache component.\n";
}
}
public function actionAssets()
{
if(empty($this->webRoot))
{
echo "Please specify a path to webRoot in command properties.\n";
Yii::app()->end();
}
$this->cleanDir($this->webRoot.'/assets');
echo "Done.\n";
}
public function actionRuntime()
{
$this->cleanDir(Yii::app()->getRuntimePath());
echo "Done.\n";
}
private function cleanDir($dir)
{
$di = new DirectoryIterator ($dir);
foreach($di as $d)
{
if(!$d->isDot())
{
echo "Removed ".$d->getPathname()."\n";
$this->removeDirRecursive($d->getPathname());
}
}
}
private function removeDirRecursive($dir)
{
$files = glob($dir.'*', GLOB_MARK);
foreach ($files as $file)
{
if (is_dir($file))
$this->removeDirRecursive($file);
else
unlink($file);
}
if (is_dir($dir))
rmdir($dir);
}
}
console.php,commandMap配置后不需要指定yiic shell index.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',
'import'=>array( //可以使用model等
'application.models.*',
'application.components.*',
),
'components'=>array(
'db'=>require(dirname(__FILE__) . '/db.php')
),
'commandMap' => array(
'clean' => array(
'class' => 'ext.clean_command.ECleanCommand',
'webRoot' => 'E:\Apache2\htdocs\webapp', //注意修改 class::webRoot
),
'rbac' => array(
'class' => 'application.commands.shell.RbacCommand',
)
),
);
命令行运行cd E:\Apache2\htdocs\webapp\protected\ 进入yiic.php的目录
yiic clean
Usage: E:\Apache2\htdocs\webapp\protected\yiic.php clean <action>
Actions:
cache
assets
runtime
yiic.php clean cache
yiic clean assets
E:\Apache2\htdocs\webapp\protected>yiic clean assets
Removed E:\Apache2\htdocs\webapp/assets\1f5cfc05
Removed E:\Apache2\htdocs\webapp/assets\836290cc
Done.
yiic clean runtime
单一任务
<?php
class TestCommand extends CConsoleCommand
{
public function getHelp()
{
//php.exe crons.php help test
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);
return 1; #必须返回数字
}
}
/www/yii_dev/testwebap/protected/ yiic test p1 p2 p3
你输入的参数是 :\narray(3) {
[0]=>string(2) "p1"
[1]=>string(2) "p2"
[2]=>string(2) "p3"
}
yii crontab 作业方法
yii consolecommand 控制台命令,实现定时任务。当然,这得结合系统,如XP的计划任务,linux的crontab命令 打开你的linux命令窗口,创建自动任务。至于windows系统 ,是计划任务(win系统,可以谷歌如何操作),下面只讲linux系统。
crontab -e
##然后输入
1 * * * * php /具体地址/protected/yiic.php Test >>/具体地址/protected/commands/test.log
'CException' with message 'Property "CConsoleApplication.user" is not defined.' 在console程序中不能用CWebUser,shell程序中调用到Yii::app()->user会报错