Yii scheduled task, yii_PHP tutorial

Source: Internet
Author: User
Tags schtasks
Yii scheduler task, yii. Yii scheduler task, yiiYii framework automatically generated Web application skeleton directory contains a STEP file, yiic and yiic. bat. Yiic is used on the UnixLinux platform, and yiic. bat is used on the windows platform. Yii scheduled task, yii
The Directory of the Web application skeleton automatically generated by the Yii Framework contains a STEP file, yiic and yiic. bat. Yiic is used on Unix/Linux platforms, and yiic. bat is used on windows platforms. If you want to view the help of the script, you can go to the root directory where the script is located, and then execute yiic help, which will list all available commands, it includes system commands and user-defined commands provided by Yii. If you want to know how to execute a command, you can execute the following command: 1 yiic help if you want to execute a command, you can use the following format: 1 yiic [parameters...] 1. create command console commands are stored in the directory specified by CConsoleApplication: commandPath as class files. It is stored in protected/commands by default. Each class must inherit from CConsoleCommand. The format of the class name is XyzCommand. The first letter of the command name is uppercase, and xyz is the command itself. You can configure CConsoleApplication: commandMap. The Command class can have different naming conventions and different directories. To create a new command, you can override CConsoleCommand: run () or write one or more actions. the format of the run method that overwrites the parent class can be: 1 public function run ($ args ){...} when a command is executed, the run method is called, and any parameter after the command is called will be assigned to $ args. You can use Yii: app () to call the console instance. From version 1.1.1, you can create a global command that is shared by all Yii applications on the same machine. To achieve this goal, you need to define an environment variable named YII_CONSOLE_COMMANDS, point to an existing directory, and then put the global command class in this directory. 2. console command Action a console command action is a method of the console command class. Method name format: actionXyz, the first letter of the action name is capitalized, and xyz is the called action itself. The command format for executing an action: 1 yiic -- option1 = value1 -- option2 = value2... the option-value pair next to it will be assigned to the parameter of this action method. If you give the option name without providing the corresponding value, this option will be considered as a boolean value of true. The action parameter can also declare an array type, such as: 1 public function actionIndex (array $ types ){...} the command to call it is: 1 yiic sitemap index -- types = News -- types = Article. the final command to call is: actionIndex (array ('news', 'article ')). From 1.1.6, anonymous parameters and global options are also supported. An anonymous parameter refers to a command line parameter that is not in the format of the normal option parameter (the format of options). For example: yiic sitemap index -- limit = 5 News, News is an anonymous parameter. To use an anonymous parameter, action must declare a $ args variable, such as: 1 public function actionIndex ($ limit = 10, $ args = array ()){...} $ args receives all available anonymous parameters. Global options indicates that a command line option is shared by all actions of the command. For example, a command has several actions. we want each action to have an option called verbose. we can declare a parameter named $ verbose in each action method. A better way is to declare it as the public member variable of the command class, so that verbose will become a global option. 1 class SitemapCommand extends CConsoleCommand2 {3 public $ verbose = false; 4 public function actionIndex ($ type ){...} 5} then you can execute a command with the verbose option: 1 yiic sitemap index -- verbose = 1 -- type = News3. the host machine that exits the code execution command may need to check whether our command is successfully executed, it can be identified by the exit code returned when the detection command exits. The exit code is an integer value between 0 and 254. 0 indicates that the command is successfully executed, and non-0 indicates that an error occurs during the command execution. You can exit your application by using an exit code in the action or run method. For example: 1if (/* error */) {2 return 1; // exit with error code 13} 4 //... do something... 5 return 0; // exit successfully if no return value is returned, a default value of 0 will be returned. 4. customize the default console application configuration location of the console application: protected/config/console. php. The public attributes of any CConsoleApplication can be configured in this file. This configuration file is similar to a common web application configuration file. Customizing Console Applications By default, if an application is created using the yiic webapp tool, the configuration for the console application will be protected/config/console. php. like a Web application configuration file, this file is a PHP script which returns an array representing the property initial values for a console application instance. as a result, any public property of CConsoleApplication can be configured in this file. because console commands are often created to serve for the Web application, they need to access the resources (such as DB connections) that are used by the latter. we can do so in the console application configuration file like the following: return array (...... 'components' => array ('DB' => array (......),),); as we can see, the format of the configuration is very similar to what we do in a Web application configuration. this is because both CConsoleApplication and CWebApplication share the same base class. reference: http://www.yiiframework.com/doc/guide/1.1/zh_cn/topics.console---------------------------------------------------------------------------------------------------------------------- Article: tutorial on planning tasks for PHP programs using the YII Framework 1. after you create a webapp through yiic, yiic is generated under webapp/protected. php, because it is a command line application, the yiic here. php is actually the index of webapp. like php, the command line entry file. 2. open the yiic file, add a row of settings, and add the path of the commands directory to yiic. in this way, yiic can find the command file under the commands Directory. the modified code is as follows, the code 123456 $ yiic = dirname (_ FILE _) is added in red __). '/ http://www.cnblogs.com/yii-read-only/framework/yiic.php '; $ Config = dirname (_ FILE __). '/config/console. php '; @ putenv ('yii _ CONSOLE_COMMANDS = '. dirname (_ FILE __). '/commands'); require_once ($ yiic); or: the page to be executed after configuration. This article is protected/commands/crons. phprun ();?> 3. configure the components required in produ ct/config/console. php, such as database connection. Configure main/console. php and set the import path and db connection. This part is similar to main. php. 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', 'import' => array ('application. models. * ', 'application. components. * ', 'application. components. base. * ', 'application. components. imgthumb. * ', 'application. Models. form. * ',', etc. import the class Package '), 'components' => array (// Main DB connection 'DB' => array ('ononstring' => 'MySQL: host = localhost; dbname = database name ', 'default' => true, 'username' => 'Database name', 'password' => 'Database password', 'charset' => 'utf8 ', 'tableprefix' => 'company _ ', // table prefix), 'log' => array ('class' => 'clogrouter ', 'Routes '=> array ('class' => 'cfilelogroute', 'levels' => 'Error, Warning '); 4. inherit from CConsoleCommand and write it into your own command class. Yii provides two methods for execution. if you execute a single task, write it directly in the run method, you can also write your Controller in the same way and add actionXXX. This example uses the second method, namely web program development, and adds the actionXXX method to the CConsoleCommand class to execute the program. We create a file in the commands directory to execute the task we want to execute. now we name it TestCommand. php. 4. open your linux command window and create an automatic task. For windows systems, we plan tasks (how can Google operate windows systems). here we will only talk about linux systems. Crontab-e # enter 1 *** php/specific address/protected/commands/crons. php Test>/specific address/protected/commands/test. log # The preceding command indicates that the Test task is executed every minute and the log is saved in test. log, the automation task has been completed. Scheduled Tasks for windows: schtasks/create/SC minute/mo 1/tn "taskPhp"/tr "php F: \ xampp \ htdocs \ php \ yiiblog2 \ protected \ commands \ crons. php CInsert insertData "deletes a scheduler task schtasks/delete/tn" taskPhp "and executes the insertData method in the CInsert command every one minute. See: http://www.cnlvzi.com/index.php/Index/article/id/124  http://www.yiibase.com/yii/218.html  http://www.yiiframework.com/wiki/221/cronjobsyii/  http://986866294.blog.163.com/blog/static/1651222522013571578115/

  

Http://www.bkjia.com/PHPjc/939717.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/939717.htmlTechArticleyii scheduler task, yii Yii Framework automatically generated Web application skeleton directory contains a STEP file, yiic and yiic. bat. Yiic is used on Unix/Linux platforms, and yiic. bat is used on windows platforms ....

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.