【问题标题】:Laravel 5 command - Mandatory optionsLaravel 5 命令 - 强制选项
【发布时间】:2016-11-14 18:21:18
【问题描述】:

我正在尝试编写一个 laravel 命令,但我没有强制选项。

我确实理解选项的概念是“可选的”,但我希望有一个命令,可以清楚地知道您要插入哪个输入并且没有特定的顺序。

即 我想实现这一点,par2 和 par 2 是强制性的

$command-abc --par1=value1 --par2=value2

代替:

$command-abc value1 value2

到目前为止,这是我使用的签名:

protected $signature = 'dst-comparison:analyse
                        {--client : Client Name}
                        {--clientId : Client ID}
                        {--recordingId : Client Recording ID}
                        {--CSVFile : Path to the downloaded CSV file from Spotify analytics}
                        {--dataUpdateTo=null : CSV Data will be truncated from this date onwards}';

按照 Laravel 文档 (https://laravel.com/docs/5.1/artisan) 和本指南:http://code.tutsplus.com/tutorials/your-one-stop-guide-to-laravel-commands--net-30349 似乎覆盖 getOptions 方法可以解决问题,但它对我不起作用。

/**
 * Get the console command options.
 *
 * @return array
 */
protected function getOptions()
{
    return array(
        array('client', null, InputOption::VALUE_REQUIRED, 'Client Name'),
        array('clientId', null, InputOption::VALUE_REQUIRED, 'Client ID'),
        array('recordingId', null, InputOption::VALUE_REQUIRED, 'Client Recording ID'),
        array('CSVFile', null, InputOption::VALUE_REQUIRED, 'Path to the downloaded CSV file from Spotify analytics'),
        array('dataUpdateTo', null, InputOption::VALUE_OPTIONAL, 'CSV Data will be truncated from this date onwards')
    );
}

有什么想法吗?

【问题讨论】:

  • 按照@Kjell 的建议,自己验证选项。就像你说的,选项是可选的

标签: php laravel-5 laravel-artisan


【解决方案1】:

我认为您必须自己处理强制输入。查看各种输出函数 $this->error(...) 并检查是否提供了所有必要的输入 $this->option('client');(null)

https://laravel.com/docs/master/artisan

【讨论】:

    【解决方案2】:

    如果你想让一些东西更通用并利用 Laravel 验证https://laravel.com/docs/8.x/validation,请遵循以下方法:

    • 创建一个摘要BaseCommand.php,并在其中添加验证器
    • 在您的自定义命令中扩展 BaseCommand myCustomCommand.php

    #BaseCommand.php

    <?php
    namespace Your\NameSpace;
    use Illuminate\Console\Command;
    use Validator;
    abstract class BaseCommand extends Command {
        public $rules;
        
        /**
         * Create a new command instance.
         *
         * @return void
         */
        public function __construct($rules) {
            parent::__construct();
            $this->rules = $rules;
        }
        
        /**
         * Execute the console command.
         *
         * @return mixed
         */
        public function handle() {
            if ($this->validate()) {
                $this->handleAfterValidation();
            }
        }
        
        public function validate() {
            $validator = Validator::make($this->option(), $this->rules);
            if (!$validator->passes()) {
                $this->error($validator->messages());
                return false;
            }
            
            return true;
        }
        
        abstract public function handleAfterValidation();
    }
    

    #myCustomCommand.php

    <?php
    namespace Your\NameSpace;
    use Validator;
    use Your\NameSpace\BaseCommand;
    class SendEmail extends BaseCommand {
        public $rules = [
            'email' => 'required|email',
            'age' => 'required|numeric',
            'name' => 'required|string'
        ];
        
        /**
         * The name and signature of the console command.
         *
         * @var string
         */
        protected $signature = 'mycmd:Whatever {--email=} {--age=} {--name=}';
        /**
         * The console command description.
         *
         * @var string
         */
        protected $description = 'Add description';
        
        /**
         * Create a new command instance.
         *
         * @return void
         */
        public function __construct() {
            parent::__construct($this->rules);
        }
        
        public function handleAfterValidation() {
            // DO something here with $this->option('email'), $this->option('age'), $this->option('name')
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2018-04-22
      • 2015-07-04
      • 2015-08-06
      • 2015-06-30
      • 2017-10-19
      • 1970-01-01
      • 2017-04-27
      • 2017-10-14
      • 1970-01-01
      相关资源
      最近更新 更多