How to implement Command task processing through Yii
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( '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, '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. 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"; } }
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:
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

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

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

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

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

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.

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

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

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
