如何在 Laravel 4 中设置任务调度?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31597396/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 12:01:20  来源:igfitidea点击:

How to set up task scheduling in Laravel 4?

phplaravellaravel-4croncommand

提问by Abhishek Bhatia

I've a website made in Laravel 4.2 and we have a monthly membership with 30 credits. Even if user doesn't use all of them, they should expire at valid_tilldate.

我有一个用 Laravel 4.2 制作的网站,我们有一个每月有 30 个积分的会员。即使用户没有使用所有这些,它们也应该在valid_till日期到期。

I wish to have a piece of code which will run at 12 am every night and will set credit as 0 in everyone's account who's valid_tilldate is equal to yesterday or before that.

我希望有一段代码,它将在每天晚上 12 点运行,并将在每个valid_till日期等于昨天或之前的每个帐户中设置信用为 0 。

If there could be something like task scheduling in laravel 5.1, which runs a function on particular time of day daily, that would be perfect.
I know cron can be set up in Laravel 4.2, using commands but I'm unable to understand how to use it in my case?

如果在 laravel 5.1 中可以有类似任务调度的东西,它每天在一天中的特定时间运行一个函数,那将是完美的。
我知道可以在 Laravel 4.2 中使用命令设置 cron,但我无法理解如何在我的情况下使用它?

回答by baao

Create a file like the following in app/commands, replace my comment in the fire method with your update function.

在 app/commands 中创建一个如下所示的文件,用你的更新函数替换我在 fire 方法中的注释。

<?php

use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;

class setCreditToZero extends Command {

    /**
     * The console command name.
     *
     * @var string
     */
    protected $name = 'set:zero';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description.';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function fire()
    {
        // your update function here
    }

    /**
     * Get the console command arguments.
     *
     * @return array
     */
    protected function getArguments()
    {
        return [

        ];
    }

    /**
     * Get the console command options.
     *
     * @return array
     */
    protected function getOptions()
    {
        return [

        ];
    }

}

Register your command in app/start/artisan.php

在 app/start/artisan.php 中注册您的命令

Artisan::add(new setCreditToZero);

Now set a cronjob using command on your terminal

现在在终端上使用命令设置一个 cronjob

crontab -e

And in this file set a cronjob with this line

在这个文件中用这一行设置一个 cronjob

0 0 * * * /usr/bin/php /path/to/laravel/baseDir/artisan set:zero

And you should be good to go

你应该很高兴去