Laravel 5 命令 - 强制选项
Laravel 5 command - Mandatory options
我正在尝试编写一个 laravel 命令,但我没有强制选项。
我明白选项的概念是 "optional",但我想要一个命令,其中可以清楚地知道您要插入哪个输入,并且没有特定的顺序。
即
我想实现这一点,par2 和 par 2 mandatory
$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) and this guide: 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')
);
}
有什么想法吗?
我认为您必须自己处理强制输入。查看各种输出函数 $this->error(...)
并检查是否提供了所有必要的输入 $this->option('client');
(<- returns null
如果未定义输入)
如果您想制作更通用的东西并利用 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')
}
}
我正在尝试编写一个 laravel 命令,但我没有强制选项。
我明白选项的概念是 "optional",但我想要一个命令,其中可以清楚地知道您要插入哪个输入,并且没有特定的顺序。
即 我想实现这一点,par2 和 par 2 mandatory
$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) and this guide: 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')
);
}
有什么想法吗?
我认为您必须自己处理强制输入。查看各种输出函数 $this->error(...)
并检查是否提供了所有必要的输入 $this->option('client');
(<- returns null
如果未定义输入)
如果您想制作更通用的东西并利用 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')
}
}