我想运行不同的cronjob,例如删除用户,向用户发送优惠券代码,向在指定日期之间加入的用户发送问候语等。我可以通过打开App \ Console \ Kernel.php并编写命令来执行相同的操作如下:
<div v-for="(error,key) in errors" >
<ul v-for="value in error" >
<li v-text="key+': '+value" ></li>
</ul>
</div>
还可以有人建议仅通过在控制器下而不是通过命令调用方法来运行cronjobs吗?
protected $commands = [
'\App\Console\Commands\DeleteUsers',
'\App\Console\Commands\SendCouponCode',
'\App\Console\Commands\SendGreetings',
];
protected function schedule(Schedule $schedule)
{
$schedule->command('DeleteUsers:deleteuserscronjob')->everyMinute();
$schedule->command('SendCouponCode:sendcouponcodecronjob')->everyMinute();
$schedule->command('SendGreetings:sendgreetingscronjob')->everyMinute();
}
然后
App\Http\Controllers\MyController1@MyAction1
答案 0 :(得分:1)
根据Laravel框架,使用kernel.php安排任务是正确的方法。
但是,如果要在一个控制器中执行一个方法而不是为其创建命令,则可以这样做:
protected function schedule(Schedule $schedule)
{
$schedule->call(function () {
MyController::myStaticMethod();
})->daily();
}
用于通过命令运行计划任务:
protected $commands = [
Commands\MyCommand::class,
];
protected function schedule(Schedule $schedule)
{
$schedule->command('mycommand:run')->withoutOverlapping();
}
然后在app \ Console \ Commands \ MyCommand.php中:
namespace App\Console\Commands;
use Illuminate\Console\Command;
use DB;
class MyCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'mycommand:run';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Description of my command';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
// PUT YOUR TASK TO BE RUN HERE
}
}