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!