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

How to implement Command task processing through Yii

Jun 15, 2018 am 10:50 AM
command yii Task processing

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to handle task parallelism and polling processing in PHP development How to handle task parallelism and polling processing in PHP development Oct 10, 2023 pm 12:12 PM

Title: Task parallel processing and polling implementation in PHP development In actual PHP development, processing task parallelism and polling are very common and important operations. This article will introduce how to handle parallel execution of tasks and polling processing in PHP, while providing specific code examples. 1. Task parallel processing Task parallel processing means that multiple tasks are performed at the same time without blocking each other. In PHP, there are several common ways to implement parallel processing. Multi-threaded parallel processing can achieve parallel processing of tasks through multi-threading

Ansible Ad-Hoc (peer-to-peer mode) Ansible Ad-Hoc (peer-to-peer mode) Feb 18, 2024 pm 04:48 PM

Official documentation: https://docs.ansible.com/ansible/latest/command_guide/intro_adhoc.html Introduction Ad-hoc command is a command that is temporarily entered and executed, usually used for testing and debugging. They do not need to be saved permanently. Simply put, ad-hoc is "instant command". Commonly used modules 1. command module (default module) The default module is not as powerful as the shell. Basically, the shell module can support the functions of the command module. 【1】Help ansible-doccommand# It is recommended to use the following ansible-doccomm

How to use PHP framework Yii to develop a highly available cloud backup system How to use PHP framework Yii to develop a highly available cloud backup system Jun 27, 2023 am 09:04 AM

With the continuous development of cloud computing technology, data backup has become something that every enterprise must do. In this context, it is particularly important to develop a highly available cloud backup system. The PHP framework Yii is a powerful framework that can help developers quickly build high-performance web applications. The following will introduce how to use the Yii framework to develop a highly available cloud backup system. Designing the database model In the Yii framework, the database model is a very important part. Because the data backup system requires a lot of tables and relationships

Symfony vs Yii2: Which framework is better for developing large-scale web applications? Symfony vs Yii2: Which framework is better for developing large-scale web applications? Jun 19, 2023 am 10:57 AM

As the demand for web applications continues to grow, developers have more and more choices in choosing development frameworks. Symfony and Yii2 are two popular PHP frameworks. They both have powerful functions and performance, but when faced with the need to develop large-scale web applications, which framework is more suitable? Next we will conduct a comparative analysis of Symphony and Yii2 to help you make a better choice. Basic Overview Symphony is an open source web application framework written in PHP and is built on

How to use Yii3 framework in php? How to use Yii3 framework in php? May 31, 2023 pm 10:42 PM

As the Internet continues to develop, the demand for web application development is also getting higher and higher. For developers, developing applications requires a stable, efficient, and powerful framework, which can improve development efficiency. Yii is a leading high-performance PHP framework that provides rich features and good performance. Yii3 is the next generation version of the Yii framework, which further optimizes performance and code quality based on Yii2. In this article, we will introduce how to use Yii3 framework to develop PHP applications.

Data query in Yii framework: access data efficiently Data query in Yii framework: access data efficiently Jun 21, 2023 am 11:22 AM

The Yii framework is an open source PHP Web application framework that provides numerous tools and components to simplify the process of Web application development, of which data query is one of the important components. In the Yii framework, we can use SQL-like syntax to access the database to query and manipulate data efficiently. The query builder of the Yii framework mainly includes the following types: ActiveRecord query, QueryBuilder query, command query and original SQL query

Yii2 vs Phalcon: Which framework is better for developing graphics rendering applications? Yii2 vs Phalcon: Which framework is better for developing graphics rendering applications? Jun 19, 2023 am 08:09 AM

In the current information age, big data, artificial intelligence, cloud computing and other technologies have become the focus of major enterprises. Among these technologies, graphics card rendering technology, as a high-performance graphics processing technology, has received more and more attention. Graphics card rendering technology is widely used in game development, film and television special effects, engineering modeling and other fields. For developers, choosing a framework that suits their projects is a very important decision. Among current languages, PHP is a very dynamic language. Some excellent PHP frameworks such as Yii2, Ph

Yii2 Programming Guide: How to run Cron service Yii2 Programming Guide: How to run Cron service Sep 01, 2023 pm 11:21 PM

If you're asking "What is Yii?" check out my previous tutorial: Introduction to the Yii Framework, which reviews the benefits of Yii and outlines what's new in Yii 2.0, released in October 2014. Hmm> In this Programming with Yii2 series, I will guide readers in using the Yii2PHP framework. In today's tutorial, I will share with you how to leverage Yii's console functionality to run cron jobs. In the past, I've used wget - a web-accessible URL - in a cron job to run my background tasks. This raises security concerns and has some performance issues. While I discussed some ways to mitigate the risk in our Security for Startup series, I had hoped to transition to console-driven commands

See all articles