Home >Backend Development >PHP Tutorial >Implementing scheduled tasks (cron) in Yii2.0_PHP tutorial
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, it is a Linux script and does not require PHP to run.
Create a controller in console/controllers
Create a controller in the console/controllers folder
I have created as TestController.php
I created a file called TestController.php
<?php
namespace
consolecontrollers;
use
yiiconsoleController;
/**
* Test controller
*/
class
TestController
extends
Controller {
public
function
actionIndex() {
echo
"cron service runnning"
;
}
public
function
actionMail(
$to
) {
echo
"Sending mail to "
.
$to
;
}
}
This controller should be use the console controller name space
This controller should use the command line controller namespace
use yiiconsoleController;
run it as
Use the following method to run
yii test
I have test it on windows by running
I run it in windows as follows
D:xampphtdocsyii2>d:xamppphpphp yii test
cron service runnning
D:xampphtdocsyii2>
yii test/mail [--to="hemctest@gmail.com"]
in windows for test it run as
Test in windows as follows
D:xampphtdocsyii2>d:xamppphpphp yii
test
/mail
[--to=
"hemctest@gmail.com"
]
Sending mail to [--to=hemctest@gmail.com]
The official command line application development documentation can be found here
Original English text: How to implement cron in Yii 2
This article is published by Xinyi Network, which specializes in website construction in Chengdu. For more information about yii, please pay attention to Xinyi Network’s subsequent releases. Xinyi Network’s official website is http://www.ir58.com