Laravel 5 命令调度程序,如何传递选项
Laravel 5 Command Scheduler, How to Pass in Options
我有一个命令需要几天时间作为一个选项。我没有在调度程序文档中的任何地方看到如何传递选项。是否可以将选项传递给命令调度程序?
这是我的带有天数选项的命令:
php artisan users:daysInactiveInvitation --days=30
预定为:
$schedule->command('users:daysInactiveInvitation')->daily();
最好我可以按照以下方式传递选项:
$schedule->command('users:daysInactiveInvitation')->daily()->options(['days'=>30]);
您可以在 command()
函数中提供它们。给定的字符串实际上只是 运行 通过 artisan,就像您通常自己在终端中 运行 命令一样。
$schedule->command('users:daysInactiveInvitation --days=30')->daily();
见https://github.com/laravel/framework/blob/5.0/src/Illuminate/Console/Scheduling/Schedule.php#L36
您也可以试试这个作为替代方法:
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Mail;
class WeeklySchemeofWorkSender extends Command
{
protected $signature = 'WeeklySchemeofWorkSender:sender {email} {name}';
public function handle()
{
$email = $this->argument('email');
$name = $this->argument('name');
Mail::send([],[],function($message) use($email,$name) {
$message->to($email)->subject('You have a reminder')->setBody('hi ' . $name . ', Remember to submit your work my friend!');
});
}
}
在你的Kernel.php
protected function schedule(Schedule $schedule)
{
/** Run a loop here to retrieve values for name and email **/
$name = 'Dio';
$email = 'dio@theworld.com';
/** pass the variables as an array **/
$schedule->command('WeeklySchemeofWorkSender:sender',[$email,$name])
->everyMinute();
}
我有一个命令需要几天时间作为一个选项。我没有在调度程序文档中的任何地方看到如何传递选项。是否可以将选项传递给命令调度程序?
这是我的带有天数选项的命令:
php artisan users:daysInactiveInvitation --days=30
预定为:
$schedule->command('users:daysInactiveInvitation')->daily();
最好我可以按照以下方式传递选项:
$schedule->command('users:daysInactiveInvitation')->daily()->options(['days'=>30]);
您可以在 command()
函数中提供它们。给定的字符串实际上只是 运行 通过 artisan,就像您通常自己在终端中 运行 命令一样。
$schedule->command('users:daysInactiveInvitation --days=30')->daily();
见https://github.com/laravel/framework/blob/5.0/src/Illuminate/Console/Scheduling/Schedule.php#L36
您也可以试试这个作为替代方法:
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Mail;
class WeeklySchemeofWorkSender extends Command
{
protected $signature = 'WeeklySchemeofWorkSender:sender {email} {name}';
public function handle()
{
$email = $this->argument('email');
$name = $this->argument('name');
Mail::send([],[],function($message) use($email,$name) {
$message->to($email)->subject('You have a reminder')->setBody('hi ' . $name . ', Remember to submit your work my friend!');
});
}
}
在你的Kernel.php
protected function schedule(Schedule $schedule)
{
/** Run a loop here to retrieve values for name and email **/
$name = 'Dio';
$email = 'dio@theworld.com';
/** pass the variables as an array **/
$schedule->command('WeeklySchemeofWorkSender:sender',[$email,$name])
->everyMinute();
}