在Yii2.0中實現計劃任務(cron)_PHP教程

來源:互聯網
上載者:User

以下由我們在信易網路公司開發項目的時候終結出的一些經驗

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.

在進階模版中的 yii 檔案,它是一個 Linux 指令碼,不需要使用PHP來運行。

Create cron service command

建立計劃任務服務命令

Create a controller in console/controllers

在 console/controllers 檔案夾下建立一個控制器

I have created as TestController.php

我建立了一個名為 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 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

我在 windows 下使用如下方式運行

D:\xampp\htdocs\yii2>d:\xampp\php\php yii test

cron service runnningD:\xampp\htdocs\yii2>

How to pass params

如何傳遞參數

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

in windows for test it run as

在 windows 中測試如下

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

Sending mail to [--to=hemctest@gmail.com]

官方命令列應用開發文檔見此

英文原文: How to implement cron in Yii 2

本文由專註於成都網站建設的信易網路發布,更多關於yii的資訊請關注信易網路隨後的發布,信易網路的官網http://www.ir58.com

http://www.bkjia.com/PHPjc/770659.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/770659.htmlTechArticle以下由我們在信易網路公司開發項目的時候終結出的一些經驗 Create console application 建立命令列應用 In advance template there is already a file yii....

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.