我创建了一个带有需要多个参数的方法的Task类:
class Sample_Task
{
public function create($arg1, $arg2) {
// something here
}
}
但似乎工匠只得到了第一个论点:
php artisan sample:create arg1 arg2
错误消息:
Warning: Missing argument 2 for Sample_Task::create()
如何在此方法中传递多个参数?
发布于 2013-01-18 18:36:08
class Sample_Task
{
public function create($args) {
$arg1 = $args[0];
$arg2 = $args[1];
// something here
}
}
发布于 2016-08-20 17:59:18
Laravel 5.2
您需要做的是将$signature
属性中的参数(或选项,例如--)指定为数组选项。Laravel用星号表示这一点。
参数
例如,假设你有一个Artisan命令来“处理”图像:
protected $signature = 'image:process {id*}';
如果你这样做了:
php artisan help image:process
…Laravel将负责添加正确的Unix风格的语法:
Usage:
image:process <id> (<id>)...
要在handle()
方法中访问该列表,只需使用:
$arguments = $this->argument('id');
foreach($arguments as $arg) {
...
}
选项
我说它也适用于选项,你可以在$signature
中使用{--id=*}
。
帮助文本将显示:
Usage:
image:process [options]
Options:
--id[=ID] (multiple values allowed)
-h, --help Display this help message
...
因此,用户将键入:
php artisan image:process --id=1 --id=2 --id=3
要访问handle()
中的数据,您需要使用:
$ids = $this->option('id');
如果你省略了'id',你会得到所有的选项,包括布尔值,'quiet','verbose‘等等。
$options = $this->option();
您可以在$options['id']
中访问ID列表
有关详细信息,请访问Laravel Artisan guide。
https://stackoverflow.com/questions/14394597
复制相似问题