Home  >  Article  >  php教程  >  Detailed explanation of the method of implementing Command task processing in Yii

Detailed explanation of the method of implementing Command task processing in Yii

高洛峰
高洛峰Original
2016-12-30 16:19:261503browse

The example in this article describes how Yii implements Command task processing. Share it with everyone for your reference, the details are as follows:

1. Configuration, components required to perform the task

Task configuration file:/protected/config/console.php

The configuration method is similar to configuring the main file

<?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. The task file

is placed in the /protected/commands/ file directory and inherits CConsoleCommand. The base class’s task file naming method is Task name+Command

For example 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. Execute the task

Open the command line tool, enter the /protected directory of the project and enter the yiic command. A prompt will appear. The prompt list shows what you just wrote. Task file

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:

Execute the command yiic go to realize task processing

I hope this article will be helpful to everyone’s PHP programming based on the Yii framework.

For more detailed explanations of how Yii implements Command task processing, please pay attention to the PHP Chinese website for related articles!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn