如何在Laravel 4中设置任务调度? [英] How to set up task scheduling in Laravel 4?
问题描述
我有一个网站在Laravel 4.2,我们有一个月会员30个学分。即使用户不使用它们,他们应该在 valid_till
日期到期。
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_till
date.
我希望有一段代码,它将在每天晚上12点运行,并在每个人的帐户中将信用值设置为0 valid_till
date等于昨天或之前。
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_till
date is equal to yesterday or before that.
如果在laravel 5.1中可以有任务调度,它每天在特定的时间运行一个函数,这将是完美的。
我知道cron可以设置在Laravel 4.2,使用命令,但我不能理解如何使用它在我的case。
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?
推荐答案
在app / commands中创建一个类似下面的文件,使用更新函数替换fire方法中的注释。
Create a file like the following in app/commands, replace my comment in the fire method with your update function.
<?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 [
];
}
}
在app / start注册命令/artisan.php
Register your command in app/start/artisan.php
Artisan::add(new setCreditToZero);
现在使用终端上的命令设置cronjob
Now set a cronjob using command on your terminal
crontab -e
cronjob with this line
And in this file set a cronjob with this line
0 0 * * * /usr/bin/php /path/to/laravel/baseDir/artisan set:zero
你应该很好
这篇关于如何在Laravel 4中设置任务调度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!