Quantcast
Channel: Live News for Yii Framework
Viewing all articles
Browse latest Browse all 3375

[Wiki] How to implement cron in Yii 2

$
0
0

Create console application

In advance template there is already a file yii. And there is no need to run it as php, it is Linux script.

Create cron service command

Create a controller in console/controllers

I have created as TestController.php

<?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 should be use the console controller name space

use yii\console\Controller;

How to run it

run it as

yii test

I have test it on windows by running

D:\xampp\htdocs\yii2>d:\xampp\php\php yii test
cron service runnning
D:\xampp\htdocs\yii2>

How to pass params

yii test/mail [--to="hemctest@gmail.com"]

in windows for test it run as.

D:\xampp\htdocs\yii2>d:\xampp\php\php yii test/mail [--to="hemctest@gmail.com"]
Sending mail to [--to=hemctest@gmail.com]

Official docs for console applications can be seen here.


Viewing all articles
Browse latest Browse all 3375