Implement the scheduled task (cron) in Yii2.0 ). The following are some of the experiences we have gained when developing projects in Xinyi network. Createconsoleapplication creates the command line application Inadvancetemplatethereisalreadyafileyii. the following are some of the experiences we have gained when developing projects in Xinyi network company: Create console application Create command line application
In advance template there is already a file yii. And there is no need to run it as php, it is Linux script.
The yii file in the advanced template is a Linux script and does not need to be run using PHP.
Create cron service command Create a scheduled task service command
Create a controller in console/controllers
Create a controller in the console/controllers folder
I have created as TestController. php
I created a file named TestController. php.
namespace
console\controllers;
use
yii\console\Controller;
/**
* Test controller
*/
class
TestController
extends
Controller {
public
function
actionIndex() {
echo
"cron service runnning"
;
}
public
function
actionMail(
$to
) {
echo
"Sending mail to "
.
$to
;
}
}
This controller shoshould be used the console controller name space
This controller should use the namespace of the command line controller
use yii\console\Controller;
How to run itRun it
Run as follows:
yii test
I have test it on windows by running
Run the following command in windows:
D:\xampp\htdocs\yii2>d:\xampp\php\php yii test
cron service runnning
D:\xampp\htdocs\yii2>
How to pass params How to pass parametersyii test/mail [--to="hemctest@gmail.com"]
In windows for test it run
Test the following in windows:
D:\xampp\htdocs\yii2>d:\xampp\php\php yii
test
/mail
[--to=
"hemctest@gmail.com"
]
Sending mail to [--to=hemctest@gmail.com]
For the official command line application development documentation, see this
How to implement cron in Yii 2
This article is published by Xinyi network, which focuses on website construction in Chengdu,