• 技术文章 >php教程 >php手册

    在Yii2.0中实现计划任务(cron)

    2016-06-13 09:35:08原创730

    以下由我们在信易网络公司开发项目的时候终结出的一些经验

    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 runnning D:\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

    声明:本文原创发布php中文网,转载请注明出处,感谢您的尊重!如有疑问,请联系admin@php.cn处理
    上一篇:php实例分享之通过递归实现删除目录下的所有文件详解 下一篇:php的慢速日志引起的Mysql错误问题分析
    大前端线上培训班

    相关文章推荐

    • php中实现api接口思路介绍 • php的memcached扩展• php 备份数据库代码(生成word,excel,json,xml,sql)• php Smarty 字符比较代码• PHP及Zend Engine的线程安全模型分析

    全部评论我要评论

  • 取消发布评论发送
  • 1/1