How to implement a scheduled task (cron) _ PHP in Yii2.0

Source: Internet
Author: User
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 it

Run 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 runnningD:\xampp\htdocs\yii2>How to pass params How to pass parameters

yii 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,

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.