• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

如何通过Yii实现Command任务处理的方法

php 搞代码 3年前 (2022-01-22) 21次浏览 已收录 0个评论

这篇文章主要介绍了Yii实现Command任务处理的方法,结合实例形式分析了Yii配置、加载及使用Command任务处理的步骤与相关技巧,需要的朋友可以参考下

本文实例讲述了Yii实现Command任务处理的方法。分享给大家供大家参考,具体如下:

1.配置,执行任务所需要的组件

任务配置文件:/protected/config/console.php

配置方法跟配置main文件差不多

<?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,  <span style="color:transparent">/本文来源gaodai#ma#com搞*!代#%^码网%</span><sub>搞代gaodaima码</sub>        '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

<?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命令即出现提示,提示列表显示刚才写的任务文件

E:\project\app\protected>yiicYii 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- webappTo see inpidual command help, use the following:

执行命令 yiic go 可实现任务处理

以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请随时关注我们网站!

相关推荐:

关于Yii中CDBCriteria的常用方法

如何解决Yii2下session跨域名共存的问题

以上就是如何通过Yii实现Command任务处理的方法的详细内容,更多请关注搞代码gaodaima其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:如何通过Yii实现Command任务处理的方法

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址