首页 > 活着 > YII Console

YII Console

可以用来做定时程序通过命令运行代码,非url访问方式更安全

1.yiic 命令用到的是yiic.php
2.控制台的命令配置文件是应用的protected/config/console.php文件,系统默认的路径是protected/commands/shell 如果你执行单一的任务,直接在run方法里面写,另外一种就是同写你的Controller(控制器),前面增加actionXXX

protected/extensions/clean_command/ECleanCommand.php

Java代码
1. 2.class ECleanCommand extends CConsoleCommand
3.{
4. public $webRoot = null;
5. public function getHelp()
6. {
7. $out = "Clean command allows you to clean up various temporary data Yii and an application are generating.\n\n";
8. return $out.parent::getHelp();
9. }
10. public function actionCache()
11. {
12. $cache=Yii::app()->getComponent(‘cache’);
13. if($cache!==null){
14. $cache->flush();
15. echo “Done.\n”;
16. }
17. else {
18. echo “Please configure cache component.\n”;
19. }
20. }
21. public function actionAssets()
22. {
23. if(empty($this->webRoot))
24. {
25. echo “Please specify a path to webRoot in command properties.\n”;
26. Yii::app()->end();
27. }
28. $this->cleanDir($this->webRoot.’/assets’);
29. echo “Done.\n”;
30. }
31. public function actionRuntime()
32. {
33. $this->cleanDir(Yii::app()->getRuntimePath());
34. echo “Done.\n”;
35. }
36. private function cleanDir($dir)
37. {
38. $di = new DirectoryIterator ($dir);
39. foreach($di as $d)
40. {
41. if(!$d->isDot())
42. {
43. echo “Removed “.$d->getPathname().”\n”;
44. $this->removeDirRecursive($d->getPathname());
45. }
46. }
47. }
48. private function removeDirRecursive($dir)
49. {
50. $files = glob($dir.’*’, GLOB_MARK);
51. foreach ($files as $file)
52. {
53. if (is_dir($file))
54. $this->removeDirRecursive($file);
55. else
56. unlink($file);
57. }
58. if (is_dir($dir))
59. rmdir($dir);
60. }
61.}
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

Java代码
1. 2.
3.// This is the configuration for yiic console application.
4.// Any writable CConsoleApplication properties can be configured here.
5.return array(
6. 'basePath' => dirname(__FILE__) . DIRECTORY_SEPARATOR . ‘..’,
7. ‘name’ => ‘My Console Application’,
8.// ‘components’ => array(),
9. ‘commandMap’ => array(
10. ‘clean’ => array(
11. ‘class’ => ‘ext.clean_command.ECleanCommand’,
12. ‘webRoot’ => ‘E:\Apache2\htdocs\webapp’, //注意修改 class::webRoot
13. ),
14. ‘rbac’ => array(
15. ‘class’ => ‘application.commands.shell.RbacCommand’,
16. )
17. ),
18.);

// 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’,
// ‘components’ => array(),
‘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

Java代码
1.Usage: E:\Apache2\htdocs\webapp\protected\yiic.php clean
2.Actions:
3. cache
4. assets
5. runtime
Usage: E:\Apache2\htdocs\webapp\protected\yiic.php clean
Actions:
cache
assets
runtimeyiic clean cache
yiic clean assets

Java代码
1.E:\Apache2\htdocs\webapp\protected>yiic clean assets
2.Removed E:\Apache2\htdocs\webapp/assets\1f5cfc05
3.Removed E:\Apache2\htdocs\webapp/assets\836290cc
4.Done.
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

可以用来做定时程序通过命令运行代码,非url访问方式更安全 1.yiic 命令用到的是yiic.php 2.控 […]

  1. 还没有评论
评论提交中, 请稍候...

留言

(Spamcheck Enabled)

Trackbacks & Pingbacks ( 0 )
  1. 还没有 trackbacks