本文实例讲述了Yii实现Command任务处理的方法。分享给大家供大家参考,具体如下:
1.配置,执行任务所需要的组件
任务配置文件:/protected/config/console.php
配置方法跟配置main文件差不多
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
<?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
// 自动载入的模型和组件类
'import' => array (
'application.models.*' , //载入"application/models/"文件夹下的所有模型类
'application.components.*' , //载入"application/components/"文件夹下的所有应用组件类
'application.extensions.*' , //载入"application/extensions/"文件夹下的所有应用组件类
),
'components' => array (
// uncomment the following to use a MySQL database
'db' => array (
'connectionString' => 'mysql:host=localhost;dbname=dbname' , //连接mysql数据库
'emulatePrepare' => true,
'username' => 'root' , //MySQL数据库用户名
'password' => '123456' , //MySQL数据库用户密码
'charset' => 'utf8' , //MySQL数据库编码
'tablePrefix' => 'zd_' , //MySQL数据库表前缀
'enableProfiling' =>true,
'enableParamLogging' =>true,
),
//加载Email组件
'mailer' => array (
'class' => 'application.extensions.mailer.EMailer' ,
),
),
);
|
2.任务文件
放在 /protected/commands/ 文件目录下继承 CConsoleCommand 基类的为任务文件 命名方法为 任务名称+Command
例如 GoCommand.php
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
<?php
/**
* 自动运行文件
*/
class GoCommand extends CConsoleCommand
{
/**
* 死循环输出
*/
public function run(){
for ( $i =1; $i >0; $i ++){
self::echoWord( $i );
sleep(2); //休眠2秒
//跳出
if (i==500){
break ;
}
}
}
/**
* 输出hollo word
*/
public function echoWord( $i ){
echo "hollo word --$i\\n" ;
}
}
|
3.执行任务
打开命令行工具,进入项目的/protected 目录下 输入yiic命令即出现提示,提示列表显示刚才写的任务文件
?
1
2
3
4
5
6
7
8
9
10
11
|
E:\\project\\app\\ protected >yiic
Yii command runner (based on Yii v1.1.12)
Usage: E:\\zeee\\zyd\\ protected \\yiic.php <command-name> [parameters...]
The following commands are available:
- go
- mailqueue
- message
- migrate
- shell
- webapp
To see individual command help, use the following:
|
相关文章
猜你喜欢
- 个人网站服务器域名解析设置指南:从购买到绑定全流程 2025-06-10
- 个人网站搭建:如何挑选具有弹性扩展能力的服务器? 2025-06-10
- 个人服务器网站搭建:如何选择适合自己的建站程序或框架? 2025-06-10
- 64M VPS建站:能否支持高流量网站运行? 2025-06-10
- 64M VPS建站:怎样选择合适的域名和SSL证书? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-06-04 80
-
怎样在Windows Server 2008上监控网站流量和日志?
2025-05-25 94 -
2025-06-04 79
-
2025-05-27 51
-
2025-05-29 103
热门评论