如何在Laravel 4中设置任务调度?
How to set up task scheduling in Laravel 4?
我有一个网站是在 Laravel 4.2 制作的,我们有每月 30 个积分的会员资格。即使用户没有使用所有这些,它们也应该在 valid_till
日期过期。
我希望有一段代码可以在每晚 运行 凌晨 12 点将 valid_till
日期等于昨天或之前的每个人的帐户中的信用设置为 0。
如果 laravel 5.1 中可以有任务调度之类的功能,运行 每天在特定时间执行此功能,那就完美了。
我知道 cron 可以在 Laravel 4.2 中使用命令设置,但我无法理解如何在我的情况下使用它?
在 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 [
];
}
}
在 app/start/artisan 中注册您的命令。php
Artisan::add(new setCreditToZero);
现在在您的终端上使用命令设置一个 cronjob
crontab -e
并在此文件中使用此行设置一个 cronjob
0 0 * * * /usr/bin/php /path/to/laravel/baseDir/artisan set:zero
你应该可以开始了
我有一个网站是在 Laravel 4.2 制作的,我们有每月 30 个积分的会员资格。即使用户没有使用所有这些,它们也应该在 valid_till
日期过期。
我希望有一段代码可以在每晚 运行 凌晨 12 点将 valid_till
日期等于昨天或之前的每个人的帐户中的信用设置为 0。
如果 laravel 5.1 中可以有任务调度之类的功能,运行 每天在特定时间执行此功能,那就完美了。
我知道 cron 可以在 Laravel 4.2 中使用命令设置,但我无法理解如何在我的情况下使用它?
在 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 [
];
}
}
在 app/start/artisan 中注册您的命令。php
Artisan::add(new setCreditToZero);
现在在您的终端上使用命令设置一个 cronjob
crontab -e
并在此文件中使用此行设置一个 cronjob
0 0 * * * /usr/bin/php /path/to/laravel/baseDir/artisan set:zero
你应该可以开始了