Laravel 5 命令调度器,如何传入选项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30202268/
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
Laravel 5 Command Scheduler, How to Pass in Options
提问by zeros-and-ones
I have a command that takes in a number of days as an option. I did not see anywhere in the scheduler docs how to pass in options. Is it possible to pass options in to the command scheduler?
我有一个需要几天时间的命令作为选项。我在调度程序文档中没有看到如何传递选项。是否可以将选项传递给命令调度程序?
Here is my command with a days option:
这是我的带有 days 选项的命令:
php artisan users:daysInactiveInvitation --days=30
Scheduled it would be:
计划将是:
$schedule->command('users:daysInactiveInvitation')->daily();
Preferably I could pass in the option something along the lines of:
最好我可以按照以下方式传递选项:
$schedule->command('users:daysInactiveInvitation')->daily()->options(['days'=>30]);
回答by Wader
You can just supply them in the command()
function. The string given is literally just run through artisan as you would normally run a command in the terminal yourself.
您可以在command()
函数中提供它们。给出的字符串实际上只是通过 artisan 运行,因为您通常会自己在终端中运行命令。
$schedule->command('users:daysInactiveInvitation --days=30')->daily();
See https://github.com/laravel/framework/blob/5.0/src/Illuminate/Console/Scheduling/Schedule.php#L36
见https://github.com/laravel/framework/blob/5.0/src/Illuminate/Console/Scheduling/Schedule.php#L36
回答by Bruce Tong
You could also try this as an alternative:
你也可以试试这个作为替代:
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!');
});
}
}
And in your Kernel.php
在你的 Kernel.php 中
protected function schedule(Schedule $schedule)
{
/** Run a loop here to retrieve values for name and email **/
$name = 'Dio';
$email = '[email protected]';
/** pass the variables as an array **/
$schedule->command('WeeklySchemeofWorkSender:sender',[$email,$name])
->everyMinute();
}