Home > Backend Development > PHP Tutorial > How to implement Command task processing through Yii

How to implement Command task processing through Yii

不言
Release: 2023-04-01 09:06:02
Original
1715 people have browsed it

This article mainly introduces the method of Yii to implement Command task processing, and analyzes the steps and related techniques of configuring, loading and using Command task processing in Yii in the form of examples. Friends in need can refer to this article

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

1. Configuration, the 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;,
      ),
  ),
);
Copy after login

2. The task file

is placed in The task file naming method that inherits the CConsoleCommand base class in the /protected/commands/ file directory 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";
  }
}
Copy after login

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 displays the task file just written

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 inpidual command help, use the following:
Copy after login

Execute the command yiic go to achieve task processing

The above is the entire content of this article. I hope it will be helpful to everyone's learning. For more related content, please pay attention to PHP Chinese website!

Related recommendations:

About common methods of CDBCriteria in Yii

How to solve the problem of session cross-domain coexistence under Yii2

The above is the detailed content of How to implement Command task processing through Yii. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template