php 使用 Artisan::call() 传递非选项参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26159442/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 18:11:50  来源:igfitidea点击:

Using Artisan::call() to pass non-option arguments

phplaravelartisan

提问by GuruBob

In the shell I can create a database migration (for example) like so:

在 shell 中,我可以创建一个数据库迁移(例如),如下所示:

./artisan migrate:make --table="mytable" mymigration

Using Artisan::call() I can't work out how to pass a non-argument parameter ("mymigration" in this example). I have tried many variants of the code below:

使用 Artisan::call() 我无法弄清楚如何传递非参数参数(在本例中为“mymigration”)。我已经尝试了以下代码的许多变体:

Artisan::call('db:migrate', ['--table' => 'mytable', 'mymigration'])

Anyone got any ideas? I have been using shell_exec('./artisan ...') in the meantime but I'm not happy with the approach.

有人有任何想法吗?我一直在使用 shell_exec('./artisan ...') 但我对这种方法不满意。

采纳答案by Bower

Artisan::call('db:migrate', ['' => 'mymigration', '--table' => 'mytable'])should work.

Artisan::call('db:migrate', ['' => 'mymigration', '--table' => 'mytable'])应该管用。

Incidentally db:migrate isn't an artisan command out of the box. Are you sure this is correct?

顺便说一句, db:migrate 不是一个开箱即用的工匠命令。你确定这是正确的吗?

回答by ?lter Ka?an ?cal

In laravel 5.1 , you set options with/without values when calling an Artisan command from your PHP code.

在 laravel 5.1 中,您可以在从 PHP 代码调用 Artisan 命令时设置带/不带值的选项。

Artisan::call('your:commandname', ['--optionwithvalue' => 'youroptionvalue', '--optionwithoutvalue' => true]);

in this case, inside your artisan command;

在这种情况下,在您的工匠命令中;

$this->option('optionwithvalue'); //returns 'youroptionvalue'

$this->option('optionwithoutvalue'); //returns true

回答by orrd

The solution is different if you're using Laravel 5.1 or newer. Now what you need to do is you need to know the name that was given to the argument in the command's signature. You can find the name of the argument from your command shell by using php artisan helpfollowed by the command name.

如果您使用 Laravel 5.1 或更高版本,解决方案会有所不同。现在您需要做的是您需要知道在命令签名中为参数指定的名称。您可以通过使用php artisan help后跟命令名称从命令 shell 中找到参数的名称。

I think you meant to ask about "make:migration". So, for example php artisan help make:migrationshows you that it accepts a parameter called "name". So you can call it like this: Artisan::call('make:migration', ['name' => 'foo' ]).

我想你是想问“make:migration”。因此,例如php artisan help make:migration向您展示它接受一个名为“name”的参数。所以,你可以这样调用:Artisan::call('make:migration', ['name' => 'foo' ])

回答by Sarcastron

I know this question is pretty old but this came up first on my Google search so I'll add this here. @orrd's answer is correct but I'll also add that for cases that use an array of arguments where you use the asterisk *you need to supply the argument(s) as an array.

我知道这个问题已经很老了,但这首先出现在我的谷歌搜索中,所以我会在这里添加这个。@orrd 的答案是正确的,但对于使用参数数组的情况,我也会添加它,在这种情况下,*您需要使用星号将参数作为数组提供。

For example if you have a command that uses array arguments with a signature like:

例如,如果您有一个使用带有签名的数组参数的命令,例如:

protected $signature = 'command:do-something {arg_name*}';

In these cases you need to supply the arguments in an array when you call it.

在这些情况下,您需要在调用数组时提供数组中的参数。

$this->call('command:do-something', ['arg_name' => ['value']]);
$this->call('command:do-something', ['arg_name' => ['value', 'another-value']]);

回答by Kris Roofe

In you command you add getArguments():

在您的命令中添加 getArguments():

/**
 * Get the console command arguments.
 *
 * @return array
 */
protected function getArguments()
{
    return array(
        array('fdmprinterpath', InputArgument::REQUIRED, 'Basic slice config path'),
        array('configpath', InputArgument::REQUIRED, 'User slice config path'),
        array('gcodepath', InputArgument::REQUIRED, 'Path for the generated gcode'),
        array('tempstlpath', InputArgument::REQUIRED, 'Path for the model that will be sliced'),
        array('uid', InputArgument::REQUIRED, 'User id'),
    );
}

You can use the arguments:

您可以使用参数:

$fdmprinterpath = $this->argument('fdmprinterpath');
$configpath     = $this->argument('configpath');
$gcodepath      = $this->argument('gcodepath');
$tempstlpath    = $this->argument('tempstlpath');
$uid            = $this->argument('uid');

call you command with parameters:

用参数调用你的命令:

Artisan::call('command:slice-model', ['fdmprinterpath' => $fdmprinterpath, 'configpath' => $configpath, 'gcodepath' => $gcodepath, 'tempstlpath' => $tempstlpath]);

For more info refer to this article.

有关更多信息,请参阅本文

回答by ahmedkandil

use

Artisan::call('db:migrate', ['--table' => 'mytable', 'mymigration'=>true])