首页 php教程 PHP开发 Yii实现Command任务处理的方法详解

Yii实现Command任务处理的方法详解

Dec 30, 2016 pm 04:19 PM

本文实例讲述了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(
  &#39;basePath&#39;=>dirname(__FILE__).DIRECTORY_SEPARATOR.&#39;..&#39;,
  &#39;name&#39;=>&#39;My Console Application&#39;,
  // application components
  // 自动载入的模型和组件类
  &#39;import&#39;=>array(
      &#39;application.models.*&#39;,//载入"application/models/"文件夹下的所有模型类
      &#39;application.components.*&#39;,//载入"application/components/"文件夹下的所有应用组件类
      &#39;application.extensions.*&#39;,//载入"application/extensions/"文件夹下的所有应用组件类
  ),
  &#39;components&#39;=>array(
      // uncomment the following to use a MySQL database
      &#39;db&#39;=>array(
          &#39;connectionString&#39; => &#39;mysql:host=localhost;dbname=dbname&#39;,//连接mysql数据库
          &#39;emulatePrepare&#39; => true,
          &#39;username&#39; => &#39;root&#39;,//MySQL数据库用户名
          &#39;password&#39; => &#39;123456&#39;,//MySQL数据库用户密码
          &#39;charset&#39; => &#39;utf8&#39;,//MySQL数据库编码
          &#39;tablePrefix&#39; => &#39;zd_&#39;, //MySQL数据库表前缀
          &#39;enableProfiling&#39;=>true,
          &#39;enableParamLogging&#39;=>true,
      ),
      //加载Email组件
      &#39;mailer&#39; => array(
          &#39;class&#39;   => &#39;application.extensions.mailer.EMailer&#39;,
      ),
  ),
);
登录后复制

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>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:
登录后复制

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

希望本文所述对大家基于Yii框架的PHP程序设计有所帮助。

更多Yii实现Command任务处理的方法详解相关文章请关注PHP中文网!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

热门话题

Java教程
1664
14
CakePHP 教程
1422
52
Laravel 教程
1316
25
PHP教程
1267
29
C# 教程
1239
24