【问题标题】:Using Artisan::call() to pass non-option arguments使用 Artisan::call() 传递非选项参数
【发布时间】:2014-11-27 08:39:22
【问题描述】:

在 shell 中,我可以像这样创建一个数据库迁移(例如):

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

使用 Artisan::call() 我不知道如何传递非参数参数(本例中为“mymigration”)。我尝试了以下代码的许多变体:

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

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

【问题讨论】:

    标签: php laravel laravel-artisan


    【解决方案1】:

    Artisan::call('db:migrate', ['' => 'mymigration', '--table' => 'mytable']) 应该可以工作。

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

    【讨论】:

    • 你是对的 - 我的意思是 migrate:make - 但问题是关于一般的工匠命令。谢谢你的回答。
    • 在 Laravel 5.4 中引发错误。我会推荐 Ikos 答案。
    • 这也不适用于多个参数...关联数组必须具有唯一键...。
    • 我不使用多个参数并且从版本 5.4 开始
    【解决方案2】:

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

    我认为您的意思是询问“make:migration”。因此,例如php artisan help make:migration 表明它接受一个名为“name”的参数。所以你可以这样称呼它:Artisan::call('make:migration', ['name' => 'foo' ])

    【讨论】:

    • 这是最适合我自己的解决方案。使用 Laravel 5.3
    • 这应该是解决方案
    【解决方案3】:

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

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

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

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

    【讨论】:

    • 我认为这个答案解决了另一个问题 - 如何将非值选项传递给工匠命令?就我而言,这是一个解决方案,因为我需要将“--force”选项传递给“迁移”命令。
    • 这一款做到了!谢谢@iko!
    • 从 5.4 开始,这是唯一的方法
    【解决方案4】:

    在你的命令中添加 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'),
        );
    }
    

    您可以使用参数:

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

    用参数调用你的命令:

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

    有关更多信息,请参阅此article

    【讨论】:

      【解决方案5】:

      我知道这个问题已经很老了,但它首先出现在我的 Google 搜索中,所以我将在此处添加它。 @orrd 的答案是正确的,但我还要补充一点,对于使用星号 * 的参数数组的情况,您需要将参数作为数组提供。

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

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

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

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

      【讨论】:

        【解决方案6】:

        使用

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

        【讨论】:

          猜你喜欢
          • 2014-08-29
          • 2019-06-05
          • 2015-01-04
          • 2017-07-02
          • 2014-08-17
          • 2014-12-17
          • 1970-01-01
          • 2015-05-16
          • 1970-01-01
          相关资源
          最近更新 更多